Skip to contentSkip to conversation

pc-dimm: pre_plug "slot" and "addr" assignment

PATCH Superseded David Hildenbrand 3 participants 17 messages Jun 28, 2018 – Jun 29, 2018

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover pc-dimm: pre_plug "slot" and "addr" assignment

As requested by Igor, assign and verify "slot" and "addr" in the pre_plug handler. Factor out the compatibility handling/configuration for detecting the alignment to be used when searching for an address in guest physical memory for a memory device.

This is another part of the original series
[PATCH v4 00/14] MemoryDevice: use multi stage hotplug handlers
And is based on
[PATCH v1 0/2] memory: fix alignment checks/asserts [PATCH v4 00/12] pc-dimm: next bunch of cleanups

This refactoring is the last step before factoring out pre_plug, plug and unplug logic of memory devices completely into memory-device.c

v1 -> v2:
- dropped "machine: factor out enforce_aligned_dimm into memory_device_align"
- dropped "pc-dimm/memory-device: detect alignment internally"
- added "util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()" -- indicate the alignment and therefor simplify compat handling for Windows as we can ...
- added "pc: drop memory region alignment check for 0"
-- ... drop a check for memory_region_get_alignment(mr), allowing us to
factor out compat handling for pc DIMM/NVDIMM alignment in a clean way.
- "pc-dimm: assign and verify the "addr" property during pre_plug" -- Default to "memory_region_get_alignment(mr)" if no compat alignment has been specified.

David Hildenbrand (4):
pc-dimm: assign and verify the "slot" property during pre_plug util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()
pc: drop memory region alignment check for 0
pc-dimm: assign and verify the "addr" property during pre_plug

 hw/i386/pc.c             | 16 ++++------
 hw/mem/pc-dimm.c         | 66 ++++++++++++++++++++++------------------
 hw/ppc/spapr.c           | 14 ++++++---
 include/hw/mem/pc-dimm.h |  5 +--
 util/oslib-win32.c       |  4 +++
 5 files changed, 60 insertions(+), 45 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/4 pc-dimm: assign and verify the "slot" property during pre_plug

We can assign and verify the slot before realizing and trying to plug. reading/writing the slot property should never fail, so let's reduce error handling a bit by using &error_abort.

To do this during pre_plug, add and use (x86, ppc) pc_dimm_pre_plug().

---
 hw/i386/pc.c             |  2 ++
 hw/mem/pc-dimm.c         | 35 ++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  9 ++++++++-
 include/hw/mem/pc-dimm.h |  1 +
 4 files changed, 29 insertions(+), 18 deletions(-)
hw/i386/pc.c +2 -0
index f310040351..bf986baf91 100644
@@ -1695,6 +1695,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
         return;
     }
+
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
 }
 
 static void pc_memory_plug(HotplugHandler *hotplug_dev,
hw/mem/pc-dimm.c +18 -17
index 65843bc52a..e56c4daef2 100644
@@ -29,10 +29,27 @@
 
 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
+{
+    Error *local_err = NULL;
+    int slot;
+
+    slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
+                                   &error_abort);
+    slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
+                                 machine->ram_slots, &local_err);
+    if (local_err) {
+        goto out;
+    }
+    object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &error_abort);
+    trace_mhp_pc_dimm_assigned_slot(slot);
+out:
+    error_propagate(errp, local_err);
+}
+
 void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
                   Error **errp)
 {
-    int slot;
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
@@ -59,22 +76,6 @@ void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
     }
     trace_mhp_pc_dimm_assigned_address(addr);
 
-    slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err);
-    if (local_err) {
-        goto out;
-    }
-
-    slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
-                                 machine->ram_slots, &local_err);
-    if (local_err) {
-        goto out;
-    }
-    object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err);
-    if (local_err) {
-        goto out;
-    }
-    trace_mhp_pc_dimm_assigned_slot(slot);
-
     memory_device_plug_region(machine, mr, addr);
     vmstate_register_ram(vmstate_mr, dev);
 
hw/ppc/spapr.c +8 -1
index b32b971a14..bf012235b6 100644
@@ -3191,6 +3191,7 @@ static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
     sPAPRMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
+    Error *local_err = NULL;
     MemoryRegion *mr;
     uint64_t size;
     Object *memdev;
@@ -3216,7 +3217,13 @@ static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
     memdev = object_property_get_link(OBJECT(dimm), PC_DIMM_MEMDEV_PROP,
                                       &error_abort);
     pagesize = host_memory_backend_pagesize(MEMORY_BACKEND(memdev));
-    spapr_check_pagesize(spapr, pagesize, errp);
+    spapr_check_pagesize(spapr, pagesize, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
 }
 
 struct sPAPRDIMMState {
include/hw/mem/pc-dimm.h +1 -0
index 26ebb7d5e9..7b120416d1 100644
@@ -79,6 +79,7 @@ typedef struct PCDIMMDeviceClass {
                                                Error **errp);
 } PCDIMMDeviceClass;
 
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp);
 void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
                   Error **errp);
 void pc_dimm_unplug(DeviceState *dev, MachineState *machine);
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/4 util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()

In reply to David Hildenbrand

Let's set the alignment just like for the posix variant. This will implicitly set the alignment of the underlying memory region and therefore make memory_region_get_alignment(mr) return something > 0 for all memory backends applicable to PCDIMM/NVDIMM.

This will allow us to drop special handling in pc.c for memory_region_get_alignment(mr) == 0, as we can then assume that it is always set (and AFAICS >= getpagesize()).

For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(), therefore alignment of DIMMs will not change, and therefore also not the guest physical memory layout.

For spapr in spapr_memory_plug(), an alignment of 0 would have been used until now. Now getpagesize() is used. But as the size of DIMMs has to be multiple of 256MB on spapr, automatic address asignment will not change the layout (no memory holes will be created as the alignment change will not apply if DIMMs start/end on 256MB boundaries).

---
 util/oslib-win32.c | 4 ++++
 1 file changed, 4 insertions(+)
util/oslib-win32.c +4 -0
index bb5ad28bd3..6e87721419 100644
@@ -76,6 +76,10 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
        memory is page aligned. */
     ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
     trace_qemu_anon_ram_alloc(size, ptr);
+
+    if (ptr && align) {
+        *align = getpagesize();
+    }
     return ptr;
 }
 
Signature · 1 line
2.17.1
#8 lore raw

PATCH v2 3/4 pc: drop memory region alignment check for 0

In reply to David Hildenbrand

All applicable memory regions always have an alignment > 0. All memory backends result in file_ram_alloc() or qemu_anon_ram_alloc() getting called, setting the alignment to > 0.

So a PCDIMM memory region always has an alignment > 0. NVDIMM copy the alignment of the original memory memory region into the handcrafted memory region that will be used at this place.

So the check for 0 can be dropped and we can reduce the special handling.

Dropping this check makes factoring out of alignment handling easier as compat handling only has to look at pcmc->enforce_aligned_dimm and not care about the alignment of the memory region.

---
 hw/i386/pc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/pc.c +1 -1
index bf986baf91..934b7155b1 100644
@@ -1712,7 +1712,7 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (memory_region_get_alignment(mr) && pcmc->enforce_aligned_dimm) {
+    if (pcmc->enforce_aligned_dimm) {
         align = memory_region_get_alignment(mr);
     }
 
Signature · 1 line
2.17.1
#11 lore raw

PATCH v2 4/4 pc-dimm: assign and verify the "addr" property during pre_plug

In reply to David Hildenbrand

We can assign and verify the slot before realizing and trying to plug. reading/writing the address property should never fail, so let's reduce error handling a bit by using &error_abort. Getting access to the memory region now might however fail. So forward errors from get_memory_region() properly.

Keep tracing the assigned address for now in the plug code, as that's the point we are sure plugging succeeded and the address willa ctually be used.

As all memory devices should use the alignment of the underlying memory region for guest physical address asignment, do detection of the alignment in pc_dimm_pre_plug(), but allow pc.c to overwrite the alignment for compatibility handling.

---
 hw/i386/pc.c             | 16 ++++--------
 hw/mem/pc-dimm.c         | 53 +++++++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  7 +++---
 include/hw/mem/pc-dimm.h |  6 ++---
 4 files changed, 41 insertions(+), 41 deletions(-)
hw/i386/pc.c +5 -11
index 934b7155b1..16e4b5baff 100644
@@ -1678,7 +1678,9 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
                                Error **errp)
 {
     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
+    const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
+    const uint64_t compat_align = TARGET_PAGE_SIZE;
 
     /*
      * When -no-acpi is used with Q35 machine type, no ACPI is built,
@@ -1696,7 +1698,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev),
+                     pcmc->enforce_aligned_dimm ? NULL : &compat_align, errp);
 }
 
 static void pc_memory_plug(HotplugHandler *hotplug_dev,
@@ -1705,18 +1708,9 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
-    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-    PCDIMMDevice *dimm = PC_DIMM(dev);
-    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
-    MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (pcmc->enforce_aligned_dimm) {
-        align = memory_region_get_alignment(mr);
-    }
-
-    pc_dimm_plug(dev, MACHINE(pcms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(pcms), &local_err);
     if (local_err) {
         goto out;
     }
hw/mem/pc-dimm.c +30 -23

83 lines not on this page · raw message

hw/ppc/spapr.c +3 -4
index bf012235b6..33543c6373 100644
@@ -3150,13 +3150,12 @@ static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align, size, addr;
+    uint64_t size, addr;
     uint32_t node;
 
-    align = memory_region_get_alignment(mr);
     size = memory_region_size(mr);
 
-    pc_dimm_plug(dev, MACHINE(ms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(ms), &local_err);
     if (local_err) {
         goto out;
     }
@@ -3223,7 +3222,7 @@ static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), NULL, errp);
 }
 
 struct sPAPRDIMMState {
include/hw/mem/pc-dimm.h +3 -3
index 7b120416d1..6457b2a25f 100644
@@ -79,8 +79,8 @@ typedef struct PCDIMMDeviceClass {
                                                Error **errp);
 } PCDIMMDeviceClass;
 
-void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp);
-void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
-                  Error **errp);
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
+                      const uint64_t *compat_align, Error **errp);
+void pc_dimm_plug(DeviceState *dev, MachineState *machine, Error **errp);
 void pc_dimm_unplug(DeviceState *dev, MachineState *machine);
 #endif
Signature · 1 line
2.17.1
#12 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] pc-dimm: assign and verify the "addr" property during pre_plug

Quoted 38 lines · David Hildenbrand

We can assign and verify the slot before realizing and trying to plug. reading/writing the address property should never fail, so let's reduce error handling a bit by using &error_abort. Getting access to the memory region now might however fail. So forward errors from get_memory_region() properly.

Keep tracing the assigned address for now in the plug code, as that's the point we are sure plugging succeeded and the address willa ctually be used.

As all memory devices should use the alignment of the underlying memory region for guest physical address asignment, do detection of the alignment in pc_dimm_pre_plug(), but allow pc.c to overwrite the alignment for compatibility handling.

---
 hw/i386/pc.c             | 16 ++++--------
 hw/mem/pc-dimm.c         | 53 +++++++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  7 +++---
 include/hw/mem/pc-dimm.h |  6 ++---
 4 files changed, 41 insertions(+), 41 deletions(-)
hw/i386/pc.c +2 -0
index 934b7155b1..16e4b5baff 100644
@@ -1678,7 +1678,9 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
                                Error **errp)
 {
     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
+    const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
+    const uint64_t compat_align = TARGET_PAGE_SIZE;
 
     /*
      * When -no-acpi is used with Q35 machine type, no ACPI is built,

hw/i386/pc.c static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1702

Quoted 6 lines
hw/i386/pc.c +2 -1
@@ -1696,7 +1698,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev),
+                     pcmc->enforce_aligned_dimm ? NULL : &compat_align, errp);

Why would you pass a const pointer, rather than just a value?

Quoted 157 lines
hw/i386/pc.c +1 -10
 }
 
 static void pc_memory_plug(HotplugHandler *hotplug_dev,
@@ -1705,18 +1708,9 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
-    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-    PCDIMMDevice *dimm = PC_DIMM(dev);
-    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
-    MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (pcmc->enforce_aligned_dimm) {
-        align = memory_region_get_alignment(mr);
-    }
-
-    pc_dimm_plug(dev, MACHINE(pcms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(pcms), &local_err);
     if (local_err) {
         goto out;
     }
hw/mem/pc-dimm.c +30 -23
index e56c4daef2..8e2e8549dc 100644
@@ -29,9 +29,14 @@
 
 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
-void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
+                      const uint64_t *compat_align, Error **errp)
 {
+    PCDIMMDevice *dimm = PC_DIMM(dev);
+    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     Error *local_err = NULL;
+    MemoryRegion *mr;
+    uint64_t addr, align;
     int slot;
 
     slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
@@ -43,44 +48,46 @@ void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
     }
     object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &error_abort);
     trace_mhp_pc_dimm_assigned_slot(slot);
+
+    mr = ddc->get_memory_region(dimm, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (compat_align) {
+        align = *compat_align;
+    } else {
+        align = memory_region_get_alignment(mr);
+    }
+
+    addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
+                                    &error_abort);
+    addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
+                                       memory_region_size(mr), &local_err);
+    if (local_err) {
+        goto out;
+    }
+    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP,
+                             &error_abort);
 out:
     error_propagate(errp, local_err);
 }
 
-void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
-                  Error **errp)
+void pc_dimm_plug(DeviceState *dev, MachineState *machine, Error **errp)
 {
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
                                                               &error_abort);
     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    Error *local_err = NULL;
     uint64_t addr;
 
-    addr = object_property_get_uint(OBJECT(dimm),
-                                    PC_DIMM_ADDR_PROP, &local_err);
-    if (local_err) {
-        goto out;
-    }
-
-    addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
-                                       memory_region_size(mr), &local_err);
-    if (local_err) {
-        goto out;
-    }
-
-    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err);
-    if (local_err) {
-        goto out;
-    }
+    addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
+                                    &error_abort);
     trace_mhp_pc_dimm_assigned_address(addr);
 
     memory_device_plug_region(machine, mr, addr);
     vmstate_register_ram(vmstate_mr, dev);
-
-out:
-    error_propagate(errp, local_err);
 }
 
 void pc_dimm_unplug(DeviceState *dev, MachineState *machine)
hw/ppc/spapr.c +3 -4
index bf012235b6..33543c6373 100644
@@ -3150,13 +3150,12 @@ static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align, size, addr;
+    uint64_t size, addr;
     uint32_t node;
 
-    align = memory_region_get_alignment(mr);
     size = memory_region_size(mr);
 
-    pc_dimm_plug(dev, MACHINE(ms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(ms), &local_err);
     if (local_err) {
         goto out;
     }
@@ -3223,7 +3222,7 @@ static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), NULL, errp);
 }
 
 struct sPAPRDIMMState {
include/hw/mem/pc-dimm.h +3 -3
index 7b120416d1..6457b2a25f 100644
@@ -79,8 +79,8 @@ typedef struct PCDIMMDeviceClass {
                                                Error **errp);
 } PCDIMMDeviceClass;
 
-void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp);
-void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
-                  Error **errp);
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
+                      const uint64_t *compat_align, Error **errp);
+void pc_dimm_plug(DeviceState *dev, MachineState *machine, Error **errp);
 void pc_dimm_unplug(DeviceState *dev, MachineState *machine);
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Reviewed-by on v2 2/4
#3 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()

In reply to David Hildenbrand
Quoted 13 lines · David Hildenbrand

Let's set the alignment just like for the posix variant. This will implicitly set the alignment of the underlying memory region and therefore make memory_region_get_alignment(mr) return something > 0 for all memory backends applicable to PCDIMM/NVDIMM.

This will allow us to drop special handling in pc.c for memory_region_get_alignment(mr) == 0, as we can then assume that it is always set (and AFAICS >= getpagesize()).

For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(), therefore alignment of DIMMs will not change, and therefore also not the guest physical memory layout.

Quoted 7 lines

For spapr in spapr_memory_plug(), an alignment of 0 would have been used until now. Now getpagesize() is used. But as the size of DIMMs has to be multiple of 256MB on spapr, automatic address asignment will not change the layout (no memory holes will be created as the alignment change will not apply if DIMMs start/end on 256MB boundaries).

To the very limited extent that I'm able to review a win32 patch:

Quoted 19 lines
---
 util/oslib-win32.c | 4 ++++
 1 file changed, 4 insertions(+)
util/oslib-win32.c +4 -0
index bb5ad28bd3..6e87721419 100644
@@ -76,6 +76,10 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
        memory is page aligned. */
     ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
     trace_qemu_anon_ram_alloc(size, ptr);
+
+    if (ptr && align) {
+        *align = getpagesize();
+    }
     return ptr;
 }
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Reviewed-by on v2 3/4
#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/4] pc: drop memory region alignment check for 0

In reply to David Hildenbrand
Quoted 8 lines · David Hildenbrand

All applicable memory regions always have an alignment > 0. All memory backends result in file_ram_alloc() or qemu_anon_ram_alloc() getting called, setting the alignment to > 0.

So a PCDIMM memory region always has an alignment > 0. NVDIMM copy the alignment of the original memory memory region into the handcrafted memory region that will be used at this place.

Quoted 8 lines

So the check for 0 can be dropped and we can reduce the special handling.

Dropping this check makes factoring out of alignment handling easier as compat handling only has to look at pcmc->enforce_aligned_dimm and not care about the alignment of the memory region.

Quoted 17 lines
---
 hw/i386/pc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/pc.c +1 -1
index bf986baf91..934b7155b1 100644
@@ -1712,7 +1712,7 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (memory_region_get_alignment(mr) && pcmc->enforce_aligned_dimm) {
+    if (pcmc->enforce_aligned_dimm) {
         align = memory_region_get_alignment(mr);
     }
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#13 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] pc-dimm: assign and verify the "addr" property during pre_plug

In reply to David Gibson
Quoted 42 lines · David Gibson
Quoted 41 lines · David Hildenbrand

We can assign and verify the slot before realizing and trying to plug. reading/writing the address property should never fail, so let's reduce error handling a bit by using &error_abort. Getting access to the memory region now might however fail. So forward errors from get_memory_region() properly.

Keep tracing the assigned address for now in the plug code, as that's the point we are sure plugging succeeded and the address willa ctually be used.

As all memory devices should use the alignment of the underlying memory region for guest physical address asignment, do detection of the alignment in pc_dimm_pre_plug(), but allow pc.c to overwrite the alignment for compatibility handling.

---
 hw/i386/pc.c             | 16 ++++--------
 hw/mem/pc-dimm.c         | 53 +++++++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  7 +++---
 include/hw/mem/pc-dimm.h |  6 ++---
 4 files changed, 41 insertions(+), 41 deletions(-)
hw/i386/pc.c +2 -0
index 934b7155b1..16e4b5baff 100644
@@ -1678,7 +1678,9 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
                                Error **errp)
 {
     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
+    const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
+    const uint64_t compat_align = TARGET_PAGE_SIZE;
 
     /*
      * When -no-acpi is used with Q35 machine type, no ACPI is built,
@@ -1696,7 +1698,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
Quoted 5 lines

hw/i386/pc.c static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1702

Quoted 3 lines
hw/i386/pc.c +2 -1
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev),
+                     pcmc->enforce_aligned_dimm ? NULL : &compat_align, errp);

Why would you pass a const pointer, rather than just a value?

We could do that if we make "0" -> "no compat alignment, detect alignment yourself". That should work here.

Thanks!

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()

In reply to David Hildenbrand
Quoted 5 lines · On Thu, 28 Jun 2018 14:14:15 +0200 David Hildenbrand <[email protected]> wrote:

Let's set the alignment just like for the posix variant. This will implicitly set the alignment of the underlying memory region and therefore make memory_region_get_alignment(mr) return something > 0 for all memory backends applicable to PCDIMM/NVDIMM.

Quoted 7 lines

This will allow us to drop special handling in pc.c for memory_region_get_alignment(mr) == 0, as we can then assume that it is always set (and AFAICS >= getpagesize()).

For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(), therefore alignment of DIMMs will not change, and therefore also not the guest physical memory layout.

why not use QEMU_VMALLOC_ALIGN for consistency (on win => getpagesize()) instead of TARGET_PAGE_SIZE like linux allocator does?

Also looking at FIXME comment it notes that VirtualAlloc might have 64K alignment (though I haven't found it in VirtualAlloc manual). If that's true then we might need set *align to it to avoid auto-picked address overlap with previous allocation (not really sure about it).

Quoted 1 line

For spapr in spapr_memory_plug(), an alignment of 0 would have been used

note that align == 0 would lead to crash where QEMU_ALIGN_UP() is used, so we don't care to keep it compatible (the same like in commit 92a37a04d)

Quoted 25 lines

until now. Now getpagesize() is used. But as the size of DIMMs has to be multiple of 256MB on spapr, automatic address asignment will not change the layout (no memory holes will be created as the alignment change will not apply if DIMMs start/end on 256MB boundaries).

---
 util/oslib-win32.c | 4 ++++
 1 file changed, 4 insertions(+)
util/oslib-win32.c +4 -0
index bb5ad28bd3..6e87721419 100644
@@ -76,6 +76,10 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
        memory is page aligned. */
     ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
     trace_qemu_anon_ram_alloc(size, ptr);
+
+    if (ptr && align) {
+        *align = getpagesize();
+    }
     return ptr;
 }
 
Reviewed-by on v2 3/4
#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/4] pc: drop memory region alignment check for 0

In reply to David Hildenbrand
Quoted 8 lines · On Thu, 28 Jun 2018 14:14:16 +0200 David Hildenbrand <[email protected]> wrote:

All applicable memory regions always have an alignment > 0. All memory backends result in file_ram_alloc() or qemu_anon_ram_alloc() getting called, setting the alignment to > 0.

So a PCDIMM memory region always has an alignment > 0. NVDIMM copy the alignment of the original memory memory region into the handcrafted memory region that will be used at this place.

Quoted 8 lines

So the check for 0 can be dropped and we can reduce the special handling.

Dropping this check makes factoring out of alignment handling easier as compat handling only has to look at pcmc->enforce_aligned_dimm and not care about the alignment of the memory region.

Quoted 17 lines
---
 hw/i386/pc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/pc.c +1 -1
index bf986baf91..934b7155b1 100644
@@ -1712,7 +1712,7 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (memory_region_get_alignment(mr) && pcmc->enforce_aligned_dimm) {
+    if (pcmc->enforce_aligned_dimm) {
         align = memory_region_get_alignment(mr);
     }
 
#14 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] pc-dimm: assign and verify the "addr" property during pre_plug

In reply to David Hildenbrand
Quoted 43 lines · On Fri, 29 Jun 2018 09:14:52 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 42 lines · David Gibson
Quoted 41 lines · David Hildenbrand

We can assign and verify the slot before realizing and trying to plug. reading/writing the address property should never fail, so let's reduce error handling a bit by using &error_abort. Getting access to the memory region now might however fail. So forward errors from get_memory_region() properly.

Keep tracing the assigned address for now in the plug code, as that's the point we are sure plugging succeeded and the address willa ctually be used.

As all memory devices should use the alignment of the underlying memory region for guest physical address asignment, do detection of the alignment in pc_dimm_pre_plug(), but allow pc.c to overwrite the alignment for compatibility handling.

---
 hw/i386/pc.c             | 16 ++++--------
 hw/mem/pc-dimm.c         | 53 +++++++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  7 +++---
 include/hw/mem/pc-dimm.h |  6 ++---
 4 files changed, 41 insertions(+), 41 deletions(-)
hw/i386/pc.c +2 -0
index 934b7155b1..16e4b5baff 100644
@@ -1678,7 +1678,9 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
                                Error **errp)
 {
     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
+    const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
+    const uint64_t compat_align = TARGET_PAGE_SIZE;
 
     /*
      * When -no-acpi is used with Q35 machine type, no ACPI is built,
@@ -1696,7 +1698,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
Quoted 8 lines
Quoted 5 lines

hw/i386/pc.c static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1702

Quoted 3 lines
hw/i386/pc.c +2 -1
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev),
+                     pcmc->enforce_aligned_dimm ? NULL : &compat_align, errp);  

Why would you pass a const pointer, rather than just a value?

We could do that if we make "0" -> "no compat alignment, detect alignment yourself". That should work here.

I'd prefer pointer as it doesn't give any special meaning to 0 value and more visible showing that something non trivial happens here

Quoted 3 lines

Thanks!

#15 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] pc-dimm: assign and verify the "addr" property during pre_plug

In reply to David Hildenbrand
Quoted 8 lines · On Thu, 28 Jun 2018 14:14:17 +0200 David Hildenbrand <[email protected]> wrote:

We can assign and verify the slot before realizing and trying to plug. reading/writing the address property should never fail, so let's reduce error handling a bit by using &error_abort. Getting access to the memory region now might however fail. So forward errors from get_memory_region() properly.

Keep tracing the assigned address for now in the plug code, as that's the point we are sure plugging succeeded and the address willa ctually be

                                                       ^^^^^^^^^^^^^   
Quoted 1 line

used.

I'd move tracing to pre_plug as well, so even if pre_plug fails we could see in trace what addr was during the failure

Quoted 4 lines

As all memory devices should use the alignment of the underlying memory region for guest physical address asignment, do detection of the alignment in pc_dimm_pre_plug(), but allow pc.c to overwrite the alignment for compatibility handling.

patch look ok,
see some nits below

Quoted 13 lines
---
 hw/i386/pc.c             | 16 ++++--------
 hw/mem/pc-dimm.c         | 53 +++++++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  7 +++---
 include/hw/mem/pc-dimm.h |  6 ++---
 4 files changed, 41 insertions(+), 41 deletions(-)
hw/i386/pc.c +0 -0
index 934b7155b1..16e4b5baff 100644

hw/i386/pc.c static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1683

Quoted 6 lines
hw/i386/pc.c +2 -0
@@ -1678,7 +1678,9 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
                                Error **errp)
 {
     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
+    const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
+    const uint64_t compat_align = TARGET_PAGE_SIZE;

s/compat_align/legacy_align/

Quoted 58 lines
hw/i386/pc.c +3 -11
     /*
      * When -no-acpi is used with Q35 machine type, no ACPI is built,
@@ -1696,7 +1698,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev),
+                     pcmc->enforce_aligned_dimm ? NULL : &compat_align, errp);
 }
 
 static void pc_memory_plug(HotplugHandler *hotplug_dev,
@@ -1705,18 +1708,9 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
-    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-    PCDIMMDevice *dimm = PC_DIMM(dev);
-    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
-    MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (pcmc->enforce_aligned_dimm) {
-        align = memory_region_get_alignment(mr);
-    }
-
-    pc_dimm_plug(dev, MACHINE(pcms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(pcms), &local_err);
     if (local_err) {
         goto out;
     }
hw/mem/pc-dimm.c +7 -1
index e56c4daef2..8e2e8549dc 100644
@@ -29,9 +29,14 @@
 
 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
-void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
+                      const uint64_t *compat_align, Error **errp)
 {
+    PCDIMMDevice *dimm = PC_DIMM(dev);
+    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     Error *local_err = NULL;
+    MemoryRegion *mr;
+    uint64_t addr, align;
     int slot;
 
     slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
@@ -43,44 +48,46 @@ void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
     }
     object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &error_abort);
     trace_mhp_pc_dimm_assigned_slot(slot);
+

hw/mem/pc-dimm.c void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp) · line 61

Quoted 10 lines
hw/mem/pc-dimm.c +10 -0
+    mr = ddc->get_memory_region(dimm, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (compat_align) {
+        align = *compat_align;
+    } else {
+        align = memory_region_get_alignment(mr);
+    }

might be:

       align =  compat_align ? *compat_align : memory_region_get_alignment(mr);

hw/mem/pc-dimm.c void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp) · line 71

Quoted 10 lines
hw/mem/pc-dimm.c +10 -0
+
+    addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
+                                    &error_abort);
+    addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
+                                       memory_region_size(mr), &local_err);
+    if (local_err) {
+        goto out;
+    }
+    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP,
+                             &error_abort);

if one considers memory device as separate black box, then set might fail (there is no guaranties that later on set could return a error)

So I'd keep local_err & co for set actions through out all *_plug handlers

Quoted 89 lines
hw/mem/pc-dimm.c +3 -22
 out:
     error_propagate(errp, local_err);
 }
 
-void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
-                  Error **errp)
+void pc_dimm_plug(DeviceState *dev, MachineState *machine, Error **errp)
 {
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
                                                               &error_abort);
     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    Error *local_err = NULL;
     uint64_t addr;
 
-    addr = object_property_get_uint(OBJECT(dimm),
-                                    PC_DIMM_ADDR_PROP, &local_err);
-    if (local_err) {
-        goto out;
-    }
-
-    addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
-                                       memory_region_size(mr), &local_err);
-    if (local_err) {
-        goto out;
-    }
-
-    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err);
-    if (local_err) {
-        goto out;
-    }
+    addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
+                                    &error_abort);
     trace_mhp_pc_dimm_assigned_address(addr);
 
     memory_device_plug_region(machine, mr, addr);
     vmstate_register_ram(vmstate_mr, dev);
-
-out:
-    error_propagate(errp, local_err);
 }
 
 void pc_dimm_unplug(DeviceState *dev, MachineState *machine)
hw/ppc/spapr.c +3 -4
index bf012235b6..33543c6373 100644
@@ -3150,13 +3150,12 @@ static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
     PCDIMMDevice *dimm = PC_DIMM(dev);
     PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align, size, addr;
+    uint64_t size, addr;
     uint32_t node;
 
-    align = memory_region_get_alignment(mr);
     size = memory_region_size(mr);
 
-    pc_dimm_plug(dev, MACHINE(ms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(ms), &local_err);
     if (local_err) {
         goto out;
     }
@@ -3223,7 +3222,7 @@ static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), NULL, errp);
 }
 
 struct sPAPRDIMMState {
include/hw/mem/pc-dimm.h +3 -3
index 7b120416d1..6457b2a25f 100644
@@ -79,8 +79,8 @@ typedef struct PCDIMMDeviceClass {
                                                Error **errp);
 } PCDIMMDeviceClass;
 
-void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp);
-void pc_dimm_plug(DeviceState *dev, MachineState *machine, uint64_t align,
-                  Error **errp);
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
+                      const uint64_t *compat_align, Error **errp);
+void pc_dimm_plug(DeviceState *dev, MachineState *machine, Error **errp);
 void pc_dimm_unplug(DeviceState *dev, MachineState *machine);
 #endif
#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()

In reply to Igor Mammedov
Quoted 12 lines · Igor Mammedov
Quoted 9 lines · On Thu, 28 Jun 2018 14:14:15 +0200 David Hildenbrand <[email protected]> wrote:

Let's set the alignment just like for the posix variant. This will implicitly set the alignment of the underlying memory region and therefore make memory_region_get_alignment(mr) return something > 0 for all memory backends applicable to PCDIMM/NVDIMM.

This will allow us to drop special handling in pc.c for memory_region_get_alignment(mr) == 0, as we can then assume that it is always set (and AFAICS >= getpagesize()).

Quoted 5 lines
Quoted 3 lines

For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(), therefore alignment of DIMMs will not change, and therefore also not the guest physical memory layout.

why not use QEMU_VMALLOC_ALIGN for consistency (on win => getpagesize()) instead of TARGET_PAGE_SIZE like linux allocator does?

Sure we can do that, I wanted to match here exactly what has been written in the comment.

Quoted 5 lines

Also looking at FIXME comment it notes that VirtualAlloc might have 64K alignment (though I haven't found it in VirtualAlloc manual). If that's true then we might need set *align to it to avoid auto-picked address overlap with previous allocation (not really sure about it).

"To determine the size of a page and the allocation granularity on the host computer, use the GetSystemInfo" [1]

"The size of the region, in bytes. If the lpAddress parameter is NULL, this value is rounded up to the next page boundary. " [1]

Historically, this seems to be 64k. But it will always be at least 4k (page size). So what we could do is query the actual allocation granularity:

int get_allocation_granularity(void) {
	SYSTEM_INFO system_info;

	GetSystemInfo(&system_info);
	return system_info.dwAllocationGranularity
}

"dwAllocationGranularity: The granularity for the starting address at which virtual memory can be allocated. For more information, see VirtualAlloc." [2]

What do you think?

Quoted 5 lines
Quoted 3 lines

For spapr in spapr_memory_plug(), an alignment of 0 would have been used

note that align == 0 would lead to crash where QEMU_ALIGN_UP() is used, so we don't care to keep it compatible (the same like in commit 92a37a04d)

Good point!

[1]
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx
[2]
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx

Signature · 3 lines
Thanks,

David / dhildenb
#16 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] pc-dimm: assign and verify the "addr" property during pre_plug

In reply to Igor Mammedov
Quoted 9 lines · Igor Mammedov
Quoted 6 lines · On Thu, 28 Jun 2018 14:14:17 +0200 David Hildenbrand <[email protected]> wrote:

We can assign and verify the slot before realizing and trying to plug. reading/writing the address property should never fail, so let's reduce error handling a bit by using &error_abort. Getting access to the memory region now might however fail. So forward errors from get_memory_region() properly.

Quoted 6 lines
Quoted 2 lines

Keep tracing the assigned address for now in the plug code, as that's the point we are sure plugging succeeded and the address willa ctually be

                                                       ^^^^^^^^^^^^^   
Quoted 1 line

used.

I'd move tracing to pre_plug as well, so even if pre_plug fails we could see in trace what addr was during the failure

I can do that.

Quoted 18 lines
Quoted 5 lines

As all memory devices should use the alignment of the underlying memory region for guest physical address asignment, do detection of the alignment in pc_dimm_pre_plug(), but allow pc.c to overwrite the alignment for compatibility handling.

patch look ok,
see some nits below

Quoted 8 lines
---
 hw/i386/pc.c             | 16 ++++--------
 hw/mem/pc-dimm.c         | 53 +++++++++++++++++++++++-----------------
 hw/ppc/spapr.c           |  7 +++---
 include/hw/mem/pc-dimm.h |  6 ++---
 4 files changed, 41 insertions(+), 41 deletions(-)
Quoted 12 lines

hw/i386/pc.c static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1683

Quoted 11 lines
hw/i386/pc.c +2 -0
index 934b7155b1..16e4b5baff 100644
@@ -1678,7 +1678,9 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
                                Error **errp)
 {
     const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
+    const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
+    const uint64_t compat_align = TARGET_PAGE_SIZE;

s/compat_align/legacy_align/

agreed.

Quoted 71 lines
Quoted 59 lines
hw/i386/pc.c +3 -11
     /*
      * When -no-acpi is used with Q35 machine type, no ACPI is built,
@@ -1696,7 +1698,8 @@ static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
         return;
     }
 
-    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev), errp);
+    pc_dimm_pre_plug(dev, MACHINE(hotplug_dev),
+                     pcmc->enforce_aligned_dimm ? NULL : &compat_align, errp);
 }
 
 static void pc_memory_plug(HotplugHandler *hotplug_dev,
@@ -1705,18 +1708,9 @@ static void pc_memory_plug(HotplugHandler *hotplug_dev,
     HotplugHandlerClass *hhc;
     Error *local_err = NULL;
     PCMachineState *pcms = PC_MACHINE(hotplug_dev);
-    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-    PCDIMMDevice *dimm = PC_DIMM(dev);
-    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
-    MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
-    uint64_t align = TARGET_PAGE_SIZE;
     bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
 
-    if (pcmc->enforce_aligned_dimm) {
-        align = memory_region_get_alignment(mr);
-    }
-
-    pc_dimm_plug(dev, MACHINE(pcms), align, &local_err);
+    pc_dimm_plug(dev, MACHINE(pcms), &local_err);
     if (local_err) {
         goto out;
     }
hw/mem/pc-dimm.c +7 -1
index e56c4daef2..8e2e8549dc 100644
@@ -29,9 +29,14 @@
 
 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 
-void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
+void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine,
+                      const uint64_t *compat_align, Error **errp)
 {
+    PCDIMMDevice *dimm = PC_DIMM(dev);
+    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
     Error *local_err = NULL;
+    MemoryRegion *mr;
+    uint64_t addr, align;
     int slot;
 
     slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
@@ -43,44 +48,46 @@ void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp)
     }
     object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &error_abort);
     trace_mhp_pc_dimm_assigned_slot(slot);
+

hw/mem/pc-dimm.c void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp) · line 61

Quoted 10 lines
hw/mem/pc-dimm.c +10 -0
+    mr = ddc->get_memory_region(dimm, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    if (compat_align) {
+        align = *compat_align;
+    } else {
+        align = memory_region_get_alignment(mr);
+    }

might be:

Quoted 1 line
       align =  compat_align ? *compat_align : memory_region_get_alignment(mr);

Will do if it fits into a single line (it seems it does)

Quoted 11 lines

hw/mem/pc-dimm.c void pc_dimm_pre_plug(DeviceState *dev, MachineState *machine, Error **errp) · line 71

Quoted 11 lines
hw/mem/pc-dimm.c +10 -0
+
+    addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
+                                    &error_abort);
+    addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
+                                       memory_region_size(mr), &local_err);
+    if (local_err) {
+        goto out;
+    }
+    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP,
+                             &error_abort);
Quoted 4 lines

if one considers memory device as separate black box, then set might fail (there is no guaranties that later on set could return a error)

So I'd keep local_err & co for set actions through out all *_plug handlers

In this part we are still in the pc_dimm specific code. I agree that this should be done when moving this code to the memory device specific code. For now I think this is fine.

Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()

In reply to David Hildenbrand
Quoted 45 lines · On Fri, 29 Jun 2018 17:39:10 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 12 lines · Igor Mammedov
Quoted 9 lines · On Thu, 28 Jun 2018 14:14:15 +0200 David Hildenbrand <[email protected]> wrote:

Let's set the alignment just like for the posix variant. This will implicitly set the alignment of the underlying memory region and therefore make memory_region_get_alignment(mr) return something > 0 for all memory backends applicable to PCDIMM/NVDIMM.

This will allow us to drop special handling in pc.c for memory_region_get_alignment(mr) == 0, as we can then assume that it is always set (and AFAICS >= getpagesize()).

Quoted 5 lines
Quoted 3 lines

For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(), therefore alignment of DIMMs will not change, and therefore also not the guest physical memory layout.

why not use QEMU_VMALLOC_ALIGN for consistency (on win => getpagesize()) instead of TARGET_PAGE_SIZE like linux allocator does?

Sure we can do that, I wanted to match here exactly what has been written in the comment.

Quoted 5 lines

Also looking at FIXME comment it notes that VirtualAlloc might have 64K alignment (though I haven't found it in VirtualAlloc manual). If that's true then we might need set *align to it to avoid auto-picked address overlap with previous allocation (not really sure about it).

"To determine the size of a page and the allocation granularity on the host computer, use the GetSystemInfo" [1]

"The size of the region, in bytes. If the lpAddress parameter is NULL, this value is rounded up to the next page boundary. " [1]

Historically, this seems to be 64k. But it will always be at least 4k (page size). So what we could do is query the actual allocation granularity:

int get_allocation_granularity(void) {
	SYSTEM_INFO system_info;

	GetSystemInfo(&system_info);
	return system_info.dwAllocationGranularity
}
Quoted 5 lines

"dwAllocationGranularity: The granularity for the starting address at which virtual memory can be allocated. For more information, see VirtualAlloc." [2]

What do you think?

maybe do following:

*align = MAX(get_allocation_granularity(), getpagesize())

Quoted 14 lines
Quoted 6 lines
Quoted 4 lines

For spapr in spapr_memory_plug(), an alignment of 0 would have been used

note that align == 0 would lead to crash where QEMU_ALIGN_UP() is used, so we don't care to keep it compatible (the same like in commit 92a37a04d)

Good point!

[1]
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx
[2]
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx

#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] util/oslib-win32: indicate alignment for qemu_anon_ram_alloc()

Quoted 48 lines · Igor Mammedov
Quoted 45 lines · On Fri, 29 Jun 2018 17:39:10 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 12 lines · Igor Mammedov
Quoted 9 lines · On Thu, 28 Jun 2018 14:14:15 +0200 David Hildenbrand <[email protected]> wrote:

Let's set the alignment just like for the posix variant. This will implicitly set the alignment of the underlying memory region and therefore make memory_region_get_alignment(mr) return something > 0 for all memory backends applicable to PCDIMM/NVDIMM.

This will allow us to drop special handling in pc.c for memory_region_get_alignment(mr) == 0, as we can then assume that it is always set (and AFAICS >= getpagesize()).

Quoted 5 lines
Quoted 3 lines

For pc in pc_memory_plug(), under Windows TARGET_PAGE_SIZE == getpagesize(), therefore alignment of DIMMs will not change, and therefore also not the guest physical memory layout.

why not use QEMU_VMALLOC_ALIGN for consistency (on win => getpagesize()) instead of TARGET_PAGE_SIZE like linux allocator does?

Sure we can do that, I wanted to match here exactly what has been written in the comment.

Quoted 5 lines

Also looking at FIXME comment it notes that VirtualAlloc might have 64K alignment (though I haven't found it in VirtualAlloc manual). If that's true then we might need set *align to it to avoid auto-picked address overlap with previous allocation (not really sure about it).

"To determine the size of a page and the allocation granularity on the host computer, use the GetSystemInfo" [1]

"The size of the region, in bytes. If the lpAddress parameter is NULL, this value is rounded up to the next page boundary. " [1]

Historically, this seems to be 64k. But it will always be at least 4k (page size). So what we could do is query the actual allocation granularity:

int get_allocation_granularity(void) {
	SYSTEM_INFO system_info;

	GetSystemInfo(&system_info);
	return system_info.dwAllocationGranularity
}
Quoted 8 lines
Quoted 5 lines

"dwAllocationGranularity: The granularity for the starting address at which virtual memory can be allocated. For more information, see VirtualAlloc." [2]

What do you think?

maybe do following:

*align = MAX(get_allocation_granularity(), getpagesize())

That sounds like the best alternative when having to guess what is actually going on behind the curtains.

Signature · 3 lines
Thanks,

David / dhildenb