Skip to contentSkip to conversation

qom: Allow object to be aligned

PATCH Superseded Richard Henderson 3 participants 15 messages Sep 15, 2020 – Sep 16, 2020 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover qom: Allow object to be aligned

I've seen some failures on arm and s390x hosts after enabling host vector support. It turns out that the malloc for these hosts does not provide 16-byte alignment.

We already have a function that can alloc with alignment, but we need to pass this down from the structure. We also don't want to use this function unconditionally, because the windows version does page allocation, which would be overkill for the vast majority of the objects allocated.

r~

Cc Paolo Bonzini <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Peter Maydell <[email protected]>
Cc David Gibson <[email protected]>
Cc Alistair Francis <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Cornelia Huck <[email protected]>

Richard Henderson (5):
qom: Allow objects to be allocated with increased alignment target/arm: Set instance_align on CPUARM TypeInfo target/ppc: Set instance_align on PowerPCCPU TypeInfo target/riscv: Set instance_align on RISCVCPU TypeInfo target/s390x: Set instance_align on S390CPU TypeInfo

 include/qom/object.h            |  4 ++++
 qom/object.c                    | 16 +++++++++++++---
 target/arm/cpu.c                |  2 ++
 target/riscv/cpu.c              |  1 +
 target/s390x/cpu.c              |  1 +
 target/ppc/translate_init.c.inc |  1 +
 6 files changed, 22 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/5 qom: Allow objects to be allocated with increased alignment

It turns out that some hosts have a default malloc alignment less than that required for vectors.

We assume that, with compiler annotation on CPUArchState, that we can properly align the vector portion of the guest state. Fix the alignment of the allocation by using qemu_memalloc when required.

---
Cc: Paolo Bonzini <[email protected]>
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: Eduardo Habkost <[email protected]>

---
 include/qom/object.h |  4 ++++
 qom/object.c         | 16 +++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
include/qom/object.h +4 -0
index 056f67ab3b..d52d0781a3 100644
@@ -770,6 +770,9 @@ struct Object
  * @instance_size: The size of the object (derivative of #Object).  If
  *   @instance_size is 0, then the size of the object will be the size of the
  *   parent object.
+ * @instance_align: The required alignment of the object.  If @instance_align
+ *   is 0, then normal malloc alignment is sufficient; if non-zero, then we
+ *   must use qemu_memalign for allocation.
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
@@ -807,6 +810,7 @@ struct TypeInfo
     const char *parent;
 
     size_t instance_size;
+    size_t instance_align;
     void (*instance_init)(Object *obj);
     void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
qom/object.c +13 -3
index 387efb25eb..2e53cb44a6 100644
@@ -50,6 +50,7 @@ struct TypeImpl
     size_t class_size;
 
     size_t instance_size;
+    size_t instance_align;
 
     void (*class_init)(ObjectClass *klass, void *data);
     void (*class_base_init)(ObjectClass *klass, void *data);
@@ -114,6 +115,7 @@ static TypeImpl *type_new(const TypeInfo *info)
 
     ti->class_size = info->class_size;
     ti->instance_size = info->instance_size;
+    ti->instance_align = info->instance_align;
 
     ti->class_init = info->class_init;
     ti->class_base_init = info->class_base_init;
@@ -691,13 +693,21 @@ static void object_finalize(void *data)
 static Object *object_new_with_type(Type type)
 {
     Object *obj;
+    size_t size, align;
 
     g_assert(type != NULL);
     type_initialize(type);
 
-    obj = g_malloc(type->instance_size);
-    object_initialize_with_type(obj, type->instance_size, type);
-    obj->free = g_free;
+    size = type->instance_size;
+    align = type->instance_align;
+    if (align) {
+        obj = qemu_memalign(align, size);
+    } else {
+        obj = g_malloc(size);
+    }
+
+    object_initialize_with_type(obj, size, type);
+    obj->free = (align ? qemu_vfree : g_free);
 
     return obj;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 64 lines · Richard Henderson

It turns out that some hosts have a default malloc alignment less than that required for vectors.

We assume that, with compiler annotation on CPUArchState, that we can properly align the vector portion of the guest state. Fix the alignment of the allocation by using qemu_memalloc when required.

---
Cc: Paolo Bonzini <[email protected]>
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: Eduardo Habkost <[email protected]>

---
 include/qom/object.h |  4 ++++
 qom/object.c         | 16 +++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
include/qom/object.h +4 -0
index 056f67ab3b..d52d0781a3 100644
@@ -770,6 +770,9 @@ struct Object
  * @instance_size: The size of the object (derivative of #Object).  If
  *   @instance_size is 0, then the size of the object will be the size of the
  *   parent object.
+ * @instance_align: The required alignment of the object.  If @instance_align
+ *   is 0, then normal malloc alignment is sufficient; if non-zero, then we
+ *   must use qemu_memalign for allocation.
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
@@ -807,6 +810,7 @@ struct TypeInfo
     const char *parent;
 
     size_t instance_size;
+    size_t instance_align;
     void (*instance_init)(Object *obj);
     void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
qom/object.c +3 -0
index 387efb25eb..2e53cb44a6 100644
@@ -50,6 +50,7 @@ struct TypeImpl
     size_t class_size;
 
     size_t instance_size;
+    size_t instance_align;
 
     void (*class_init)(ObjectClass *klass, void *data);
     void (*class_base_init)(ObjectClass *klass, void *data);
@@ -114,6 +115,7 @@ static TypeImpl *type_new(const TypeInfo *info)
 
     ti->class_size = info->class_size;
     ti->instance_size = info->instance_size;
+    ti->instance_align = info->instance_align;
 
     ti->class_init = info->class_init;
     ti->class_base_init = info->class_base_init;
@@ -691,13 +693,21 @@ static void object_finalize(void *data)
 static Object *object_new_with_type(Type type)
 {
     Object *obj;
+    size_t size, align;

qom/object.c static void object_finalize(void *data) · line 703

Quoted 10 lines
qom/object.c +3 -3
 
     g_assert(type != NULL);
     type_initialize(type);
 
-    obj = g_malloc(type->instance_size);
-    object_initialize_with_type(obj, type->instance_size, type);
-    obj->free = g_free;
+    size = type->instance_size;
+    align = type->instance_align;
+    if (align) {

If we check for (align > G_MEM_ALIGN) instead, we will be able to set instance_align automatically at OBJECT_DEFINE_TYPE*.

Quoted 13 lines
qom/object.c +7 -0
+        obj = qemu_memalign(align, size);
+    } else {
+        obj = g_malloc(size);
+    }
+
+    object_initialize_with_type(obj, size, type);
+    obj->free = (align ? qemu_vfree : g_free);
 
     return obj;
 }
Signature · 1 line
2.25.1
Signature · 1 line
Eduardo
#3 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 69 lines · Eduardo Habkost
Quoted 68 lines · Richard Henderson

It turns out that some hosts have a default malloc alignment less than that required for vectors.

We assume that, with compiler annotation on CPUArchState, that we can properly align the vector portion of the guest state. Fix the alignment of the allocation by using qemu_memalloc when required.

---
Cc: Paolo Bonzini <[email protected]>
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: Eduardo Habkost <[email protected]>

---
 include/qom/object.h |  4 ++++
 qom/object.c         | 16 +++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
include/qom/object.h +4 -0
index 056f67ab3b..d52d0781a3 100644
@@ -770,6 +770,9 @@ struct Object
  * @instance_size: The size of the object (derivative of #Object).  If
  *   @instance_size is 0, then the size of the object will be the size of the
  *   parent object.
+ * @instance_align: The required alignment of the object.  If @instance_align
+ *   is 0, then normal malloc alignment is sufficient; if non-zero, then we
+ *   must use qemu_memalign for allocation.
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
@@ -807,6 +810,7 @@ struct TypeInfo
     const char *parent;
 
     size_t instance_size;
+    size_t instance_align;
     void (*instance_init)(Object *obj);
     void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
qom/object.c +3 -0
index 387efb25eb..2e53cb44a6 100644
@@ -50,6 +50,7 @@ struct TypeImpl
     size_t class_size;
 
     size_t instance_size;
+    size_t instance_align;
 
     void (*class_init)(ObjectClass *klass, void *data);
     void (*class_base_init)(ObjectClass *klass, void *data);
@@ -114,6 +115,7 @@ static TypeImpl *type_new(const TypeInfo *info)
 
     ti->class_size = info->class_size;
     ti->instance_size = info->instance_size;
+    ti->instance_align = info->instance_align;
 
     ti->class_init = info->class_init;
     ti->class_base_init = info->class_base_init;
@@ -691,13 +693,21 @@ static void object_finalize(void *data)
 static Object *object_new_with_type(Type type)
 {
     Object *obj;
+    size_t size, align;
 
     g_assert(type != NULL);
     type_initialize(type);
 
Quoted 9 lines

qom/object.c static void object_finalize(void *data) · line 703

Quoted 6 lines
qom/object.c +3 -3
-    obj = g_malloc(type->instance_size);
-    object_initialize_with_type(obj, type->instance_size, type);
-    obj->free = g_free;
+    size = type->instance_size;
+    align = type->instance_align;
+    if (align) {

If we check for (align > G_MEM_ALIGN) instead, we will be able to set instance_align automatically at OBJECT_DEFINE_TYPE*.

I agree a value check would be good here, as well as setting this by default.

As for the value check itself...

I see that G_MEM_ALIGN isn't actually defined in an interesting or even correct way. E.g. it doesn't take the long double type into account.

The usual mechanism is

struct s {
char pad;

  union {
    long l;
    void *p;
    double d;
    long double ld;

} u;
};

offsetof(s, u)

since all of these types are required to be taken into account by the system malloc.

E.g it doesn't take other host guarantees into account, e.g. i386-linux guarantees 16-byte alignment. This possibly dubious ABI change was made 20+ years ago with the introduction of SSE and is now set in stone.

Glibc has a "malloc-alignment.h" internal header that defaults to

MIN(2 * sizeof(size_t), __alignof__(long double))

and is overridden for i386. Sadly, it doesn't export MALLOC_ALIGNMENT.

Musl has two different malloc implementations. One has UNIT = 16; the other has SIZE_ALIGN = 4*sizeof(size_t). Both have a minimum value of 16, and this is not target-specific.

Any further comments on the subject, or should I put together something that computes the MAX of the above?

r~

#4 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 96 lines · Richard Henderson
Quoted 69 lines · Eduardo Habkost
Quoted 68 lines · Richard Henderson

It turns out that some hosts have a default malloc alignment less than that required for vectors.

We assume that, with compiler annotation on CPUArchState, that we can properly align the vector portion of the guest state. Fix the alignment of the allocation by using qemu_memalloc when required.

---
Cc: Paolo Bonzini <[email protected]>
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: Eduardo Habkost <[email protected]>

---
 include/qom/object.h |  4 ++++
 qom/object.c         | 16 +++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)
include/qom/object.h +4 -0
index 056f67ab3b..d52d0781a3 100644
@@ -770,6 +770,9 @@ struct Object
  * @instance_size: The size of the object (derivative of #Object).  If
  *   @instance_size is 0, then the size of the object will be the size of the
  *   parent object.
+ * @instance_align: The required alignment of the object.  If @instance_align
+ *   is 0, then normal malloc alignment is sufficient; if non-zero, then we
+ *   must use qemu_memalign for allocation.
  * @instance_init: This function is called to initialize an object.  The parent
  *   class will have already been initialized so the type is only responsible
  *   for initializing its own members.
@@ -807,6 +810,7 @@ struct TypeInfo
     const char *parent;
 
     size_t instance_size;
+    size_t instance_align;
     void (*instance_init)(Object *obj);
     void (*instance_post_init)(Object *obj);
     void (*instance_finalize)(Object *obj);
qom/object.c +3 -0
index 387efb25eb..2e53cb44a6 100644
@@ -50,6 +50,7 @@ struct TypeImpl
     size_t class_size;
 
     size_t instance_size;
+    size_t instance_align;
 
     void (*class_init)(ObjectClass *klass, void *data);
     void (*class_base_init)(ObjectClass *klass, void *data);
@@ -114,6 +115,7 @@ static TypeImpl *type_new(const TypeInfo *info)
 
     ti->class_size = info->class_size;
     ti->instance_size = info->instance_size;
+    ti->instance_align = info->instance_align;
 
     ti->class_init = info->class_init;
     ti->class_base_init = info->class_base_init;
@@ -691,13 +693,21 @@ static void object_finalize(void *data)
 static Object *object_new_with_type(Type type)
 {
     Object *obj;
+    size_t size, align;
 
     g_assert(type != NULL);
     type_initialize(type);
 
Quoted 9 lines

qom/object.c static void object_finalize(void *data) · line 703

Quoted 6 lines
qom/object.c +3 -3
-    obj = g_malloc(type->instance_size);
-    object_initialize_with_type(obj, type->instance_size, type);
-    obj->free = g_free;
+    size = type->instance_size;
+    align = type->instance_align;
+    if (align) {

If we check for (align > G_MEM_ALIGN) instead, we will be able to set instance_align automatically at OBJECT_DEFINE_TYPE*.

I agree a value check would be good here, as well as setting this by default.

As for the value check itself...

I see that G_MEM_ALIGN isn't actually defined in an interesting or even correct way. E.g. it doesn't take the long double type into account.

The usual mechanism is

struct s {
char pad;

  union {
    long l;
    void *p;
    double d;
    long double ld;
Quoted 7 lines

} u;
};

offsetof(s, u)

since all of these types are required to be taken into account by the system malloc.

Due to the existence of G_MEM_ALIGN, I thought GLib alignment guarantees could be weaker than malloc(). I've just learned that GLib uses system malloc() since 2.46.

Quoted 9 lines

E.g it doesn't take other host guarantees into account, e.g. i386-linux guarantees 16-byte alignment. This possibly dubious ABI change was made 20+ years ago with the introduction of SSE and is now set in stone.

Glibc has a "malloc-alignment.h" internal header that defaults to

MIN(2 * sizeof(size_t), __alignof__(long double))

Quoted 8 lines

and is overridden for i386. Sadly, it doesn't export MALLOC_ALIGNMENT.

Musl has two different malloc implementations. One has UNIT = 16; the other has SIZE_ALIGN = 4*sizeof(size_t). Both have a minimum value of 16, and this is not target-specific.

Any further comments on the subject, or should I put together something that computes the MAX of the above?

Once we move to C11, we can just use max_align_t.

While we don't move to C11, why not just use
__alignof__(union { long l; void *p; double d; long double ld;})
?

Signature · 1 line
Eduardo
#5 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 1 line · Eduardo Habkost

Once we move to C11, we can just use max_align_t.

Yes.

Quoted 3 lines

While we don't move to C11, why not just use
__alignof__(union { long l; void *p; double d; long double ld;})
?

For i386, this is 4.

r~

#6 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 10 lines · Richard Henderson
Quoted 1 line · Eduardo Habkost

Once we move to C11, we can just use max_align_t.

Yes.

Quoted 3 lines

While we don't move to C11, why not just use
__alignof__(union { long l; void *p; double d; long double ld;})
?

For i386, this is 4.

Is i386-linux the only case where there are additional alignment guarantees not covered by C99?

I would prefer a i386-linux-specific #ifdef for that case instead of guessing based on undocumented libc internals.

Signature · 1 line
Eduardo
#7 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 6 lines · Eduardo Habkost
Quoted 5 lines · Richard Henderson
Quoted 1 line · Eduardo Habkost

Once we move to C11, we can just use max_align_t.

Yes.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

While we don't move to C11, why not just use
__alignof__(union { long l; void *p; double d; long double ld;})
?

For i386, this is 4.

Is i386-linux the only case where there are additional alignment guarantees not covered by C99?

I think so.

Quoted 2 lines

I would prefer a i386-linux-specific #ifdef for that case instead of guessing based on undocumented libc internals.

I was thinking abi, not internals.

r~

#8 lore raw

Re: [PATCH 1/5] qom: Allow objects to be allocated with increased alignment

Quoted 16 lines · Richard Henderson
Quoted 6 lines · Eduardo Habkost
Quoted 5 lines · Richard Henderson
Quoted 1 line · Eduardo Habkost

Once we move to C11, we can just use max_align_t.

Yes.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

While we don't move to C11, why not just use
__alignof__(union { long l; void *p; double d; long double ld;})
?

For i386, this is 4.

Is i386-linux the only case where there are additional alignment guarantees not covered by C99?

Quoted 6 lines

I think so.

Quoted 2 lines

I would prefer a i386-linux-specific #ifdef for that case instead of guessing based on undocumented libc internals.

I was thinking abi, not internals.

I see. As long as we can point to the specification backing the assumptions in the code, I'm OK with that.

To me, it seems simpler to start with something that works on all hosts (the alignment guarantees provided by C99), and add arch-specific optimizations later.

Signature · 1 line
Eduardo
#9 lore raw

PATCH 2/5 target/arm: Set instance_align on CPUARM TypeInfo

In reply to Richard Henderson

Fix alignment of CPUARMState.vfp.zregs.

---
Cc: Peter Maydell <[email protected]>
Cc: [email protected]

---
 target/arm/cpu.c | 2 ++
 1 file changed, 2 insertions(+)
target/arm/cpu.c +2 -0
index 7b5ea65fab..a7643deab4 100644
@@ -2290,6 +2290,7 @@ void arm_cpu_register(const ARMCPUInfo *info)
     TypeInfo type_info = {
         .parent = TYPE_ARM_CPU,
         .instance_size = sizeof(ARMCPU),
+        .instance_align = __alignof__(ARMCPU),
         .instance_init = arm_cpu_instance_init,
         .class_size = sizeof(ARMCPUClass),
         .class_init = info->class_init ?: cpu_register_class_init,
@@ -2305,6 +2306,7 @@ static const TypeInfo arm_cpu_type_info = {
     .name = TYPE_ARM_CPU,
     .parent = TYPE_CPU,
     .instance_size = sizeof(ARMCPU),
+    .instance_align = __alignof__(ARMCPU),
     .instance_init = arm_cpu_initfn,
     .instance_finalize = arm_cpu_finalizefn,
     .abstract = true,
Signature · 1 line
2.25.1
#10 lore raw

PATCH 3/5 target/ppc: Set instance_align on PowerPCCPU TypeInfo

In reply to Richard Henderson

Fix alignment of CPUPPCState.vsr.

---
Cc: David Gibson <[email protected]>
Cc: [email protected]

---
 target/ppc/translate_init.c.inc | 1 +
 1 file changed, 1 insertion(+)
target/ppc/translate_init.c.inc +1 -0
index 230a062d29..accb4f2fae 100644
@@ -10960,6 +10960,7 @@ static const TypeInfo ppc_cpu_type_info = {
     .name = TYPE_POWERPC_CPU,
     .parent = TYPE_CPU,
     .instance_size = sizeof(PowerPCCPU),
+    .instance_align = __alignof__(PowerPCCPU),
     .instance_init = ppc_cpu_instance_init,
     .instance_finalize = ppc_cpu_instance_finalize,
     .abstract = true,
Signature · 1 line
2.25.1
#11 lore raw

PATCH 4/5 target/riscv: Set instance_align on RISCVCPU TypeInfo

In reply to Richard Henderson

Fix alignment of CPURISCVState.vreg.

---
Cc: Alistair Francis <[email protected]>
Cc: [email protected]

---
 target/riscv/cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/riscv/cpu.c +1 -0
index 57c006df5d..0bbfd7f457 100644
@@ -628,6 +628,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .name = TYPE_RISCV_CPU,
         .parent = TYPE_CPU,
         .instance_size = sizeof(RISCVCPU),
+        .instance_align = __alignof__(RISCVCPU),
         .instance_init = riscv_cpu_init,
         .abstract = true,
         .class_size = sizeof(RISCVCPUClass),
Signature · 1 line
2.25.1
#12 lore raw

PATCH 5/5 target/s390x: Set instance_align on S390CPU TypeInfo

In reply to Richard Henderson

Fix alignment of CPUS390XState.vregs.

---
Cc: David Hildenbrand <[email protected]>
Cc: Cornelia Huck <[email protected]>
Cc: [email protected]

---
 target/s390x/cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/s390x/cpu.c +1 -0
index 749cd548f0..e350edc9f5 100644
@@ -517,6 +517,7 @@ static const TypeInfo s390_cpu_type_info = {
     .name = TYPE_S390_CPU,
     .parent = TYPE_CPU,
     .instance_size = sizeof(S390CPU),
+    .instance_align = __alignof__(S390CPU),
     .instance_init = s390_cpu_initfn,
     .instance_finalize = s390_cpu_finalize,
     .abstract = true,
Signature · 1 line
2.25.1
#13 lore raw
In reply to Richard Henderson
Quoted 5 lines · Richard Henderson

We already have a function that can alloc with alignment, but we need to pass this down from the structure. We also don't want to use this function unconditionally, because the windows version does page allocation, which would be overkill for the vast majority of the objects allocated.

Stefan, why are we using VirtualAlloc in util/oslib-win32.c, qemu_try_memalign, instead of _aligned_malloc?

r~

#14 lore raw

Am 16.09.20 um 00:47 schrieb Richard Henderson:

Quoted 8 lines · Richard Henderson
Quoted 5 lines

We already have a function that can alloc with alignment, but we need to pass this down from the structure. We also don't want to use this function unconditionally, because the windows version does page allocation, which would be overkill for the vast majority of the objects allocated.

Stefan, why are we using VirtualAlloc in util/oslib-win32.c, qemu_try_memalign, instead of _aligned_malloc?

I think we use it because Fabrice introduced VirtualAlloc in commit 6e4255f6a65091fbe7d17bfda546e2aa1b72f9a6 (with a comment "FIXME: this is not exactly optimal solution [...]"). Maybe there was no _aligned_malloc available 15 years ago. Commit 33f002714be2ed58ed05ae3870d5ea6915df4b47 then reused VirtualAlloc for qemu_try_memalign (without a FIXME comment).

Obviously nobody cared to find a better solution until now.

Stefan