Skip to contentSkip to conversation

assorted replacement of x[mz]alloc_bytes()

PATCH Superseded Jan Beulich 4 participants 25 messages Apr 8, 2021 – Apr 15, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover assorted replacement of x[mz]alloc_bytes()

In the long run I think we want to do away with these type-unsafe interfaces, the more that they also request (typically) excess alignment. This series of entirely independent patches is eliminating the instances where it's relatively clear that they're not just "blob" allocations.

01: x86/HVM: avoid effectively open-coding xzalloc_flex_struct() 02: x86/vPMU: avoid effectively open-coding xzalloc_flex_struct() 03: x86/MCE: avoid effectively open-coding xmalloc_array() 04: x86/HVM: avoid effectively open-coding xmalloc_array() 05: x86/oprofile: avoid effectively open-coding xmalloc_array() 06: x86/IRQ: avoid over-alignment in alloc_pirq_struct() 07: EFI/runtime: avoid effectively open-coding xmalloc_array() 08: hypfs: avoid effectively open-coding xzalloc_array() 09: kexec: avoid effectively open-coding xzalloc_flex_struct() 10: video/lfb: avoid effectively open-coding xzalloc_array() 11: Arm/optee: don't open-code xzalloc_flex_struct()

Jan

#1 lore raw

PATCH 1/11 x86/HVM: avoid effectively open-coding xzalloc_flex_struct()

Drop hvm_irq_size(), which exists for just this purpose.

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/arch/x86/hvm/hvm.c +2 -1
@@ -642,7 +642,8 @@ int hvm_domain_initialise(struct domain
     d->arch.hvm.params = xzalloc_array(uint64_t, HVM_NR_PARAMS);
     d->arch.hvm.io_handler = xzalloc_array(struct hvm_io_handler,
                                            NR_IO_HANDLERS);
-    d->arch.hvm.irq = xzalloc_bytes(hvm_irq_size(nr_gsis));
+    d->arch.hvm.irq = xzalloc_flex_struct(struct hvm_irq,
+                                          gsi_assert_count, nr_gsis);
 
     rc = -ENOMEM;
     if ( !d->arch.hvm.pl_time || !d->arch.hvm.irq ||
xen/include/asm-x86/hvm/irq.h +0 -1
@@ -98,7 +98,6 @@ struct hvm_irq {
 #define hvm_pci_intx_link(dev, intx) \
     (((dev) + (intx)) & 3)
 #define hvm_domain_irq(d) ((d)->arch.hvm.irq)
-#define hvm_irq_size(cnt) offsetof(struct hvm_irq, gsi_assert_count[cnt])
 
 #define hvm_isa_irq_to_gsi(isa_irq) ((isa_irq) ? : 2)
 
Acked-by on v1 1/11
#2 lore raw

Re: [PATCH 01/11] x86/HVM: avoid effectively open-coding xzalloc_flex_struct()

Quoted 7 lines · Jan Beulich

Drop hvm_irq_size(), which exists for just this purpose.

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

#3 lore raw

PATCH 2/11 x86/vPMU: avoid effectively open-coding xzalloc_flex_struct()

In reply to Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/arch/x86/cpu/vpmu_intel.c +4 -4
@@ -461,10 +461,10 @@ static int core2_vpmu_alloc_resource(str
             goto out_err;
     }
 
-    core2_vpmu_cxt = xzalloc_bytes(sizeof(*core2_vpmu_cxt) +
-                                   sizeof(uint64_t) * fixed_pmc_cnt +
-                                   sizeof(struct xen_pmu_cntr_pair) *
-                                   arch_pmc_cnt);
+    core2_vpmu_cxt = xzalloc_flex_struct(struct xen_pmu_intel_ctxt, regs,
+                                         fixed_pmc_cnt + arch_pmc_cnt *
+                                         (sizeof(struct xen_pmu_cntr_pair) /
+                                          sizeof(*core2_vpmu_cxt->regs)));
     p = xzalloc(uint64_t);
     if ( !core2_vpmu_cxt || !p )
         goto out_err;
Acked-by on v1 2/11
#4 lore raw

Re: [PATCH 02/11] x86/vPMU: avoid effectively open-coding xzalloc_flex_struct()

Quoted 5 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -461,10 +461,10 @@ static int core2_vpmu_alloc_resource(str
             goto out_err;
     }

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_alloc_resource(str · line 468

Quoted 10 lines
xen/arch/x86/cpu/vpmu_intel.c +4 -4
 
-    core2_vpmu_cxt = xzalloc_bytes(sizeof(*core2_vpmu_cxt) +
-                                   sizeof(uint64_t) * fixed_pmc_cnt +
-                                   sizeof(struct xen_pmu_cntr_pair) *
-                                   arch_pmc_cnt);
+    core2_vpmu_cxt = xzalloc_flex_struct(struct xen_pmu_intel_ctxt, regs,
+                                         fixed_pmc_cnt + arch_pmc_cnt *
+                                         (sizeof(struct xen_pmu_cntr_pair) /
+                                          sizeof(*core2_vpmu_cxt->regs)));
     p = xzalloc(uint64_t);

However, this is very wtf, and clearly wants reworking.  I'll see if I can find some time, unless anyone else beats me to it.

~Andrew

Quoted 3 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
     if ( !core2_vpmu_cxt || !p )
         goto out_err;
#5 lore raw

Re: [PATCH 02/11] x86/vPMU: avoid effectively open-coding xzalloc_flex_struct()

Quoted 8 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

Thanks.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -461,10 +461,10 @@ static int core2_vpmu_alloc_resource(str
             goto out_err;
     }
 
Quoted 12 lines

xen/arch/x86/cpu/vpmu_intel.c static int core2_vpmu_alloc_resource(str · line 468

Quoted 9 lines
xen/arch/x86/cpu/vpmu_intel.c +4 -4
-    core2_vpmu_cxt = xzalloc_bytes(sizeof(*core2_vpmu_cxt) +
-                                   sizeof(uint64_t) * fixed_pmc_cnt +
-                                   sizeof(struct xen_pmu_cntr_pair) *
-                                   arch_pmc_cnt);
+    core2_vpmu_cxt = xzalloc_flex_struct(struct xen_pmu_intel_ctxt, regs,
+                                         fixed_pmc_cnt + arch_pmc_cnt *
+                                         (sizeof(struct xen_pmu_cntr_pair) /
+                                          sizeof(*core2_vpmu_cxt->regs)));
     p = xzalloc(uint64_t);

However, this is very wtf, and clearly wants reworking.  I'll see if I can find some time, unless anyone else beats me to it.

I thought so too while seeing this, but it's quite a bit more of a rework than I felt I'd like to tackle right away.

Jan

#6 lore raw

PATCH 3/11 x86/MCE: avoid effectively open-coding xmalloc_array()

In reply to Jan Beulich

There is a difference in generated code: xmalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/arch/x86/cpu/mcheck/mctelem.c +1 -1
@@ -345,7 +345,7 @@ void __init mctelem_init(unsigned int da
 
 	if ((mctctl.mctc_elems = xmalloc_array(struct mctelem_ent,
 	    MC_NENT)) == NULL ||
-	    (datarr = xmalloc_bytes(MC_NENT * datasz)) == NULL) {
+	    (datarr = xmalloc_array(char, MC_NENT * datasz)) == NULL) {
 		xfree(mctctl.mctc_elems);
 		printk("Allocations for MCA telemetry failed\n");
 		return;
#7 lore raw

PATCH 4/11 x86/HVM: avoid effectively open-coding xmalloc_array()

In reply to Jan Beulich

There is a difference in generated code: xmalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/arch/x86/hvm/emulate.c +2 -2
@@ -1924,7 +1924,7 @@ static int hvmemul_rep_movs(
         dgpa -= bytes - bytes_per_rep;
 
     /* Allocate temporary buffer. Fall back to slow emulation if this fails. */
-    buf = xmalloc_bytes(bytes);
+    buf = xmalloc_array(char, bytes);
     if ( buf == NULL )
         return X86EMUL_UNHANDLEABLE;
 
@@ -2037,7 +2037,7 @@ static int hvmemul_rep_stos(
         for ( ; ; )
         {
             bytes = *reps * bytes_per_rep;
-            buf = xmalloc_bytes(bytes);
+            buf = xmalloc_array(char, bytes);
             if ( buf || *reps <= 1 )
                 break;
             *reps >>= 1;
#8 lore raw

PATCH 5/11 x86/oprofile: avoid effectively open-coding xmalloc_array()

In reply to Jan Beulich

There is a difference in generated code: xmalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

While at it also use XFREE() instead of open-coding it and change loop induction variable types.

xen/arch/x86/oprofile/nmi_int.c +9 -10
@@ -142,30 +142,29 @@ static void nmi_save_registers(void * du
 
 static void free_msrs(void)
 {
-	int i;
+	unsigned int i;
+
 	for (i = 0; i < nr_cpu_ids; ++i) {
-		xfree(cpu_msrs[i].counters);
-		cpu_msrs[i].counters = NULL;
-		xfree(cpu_msrs[i].controls);
-		cpu_msrs[i].controls = NULL;
+		XFREE(cpu_msrs[i].counters);
+		XFREE(cpu_msrs[i].controls);
 	}
 }
 
 
 static int allocate_msrs(void)
 {
+	unsigned int i;
 	int success = 1;
-	size_t controls_size = sizeof(struct op_msr) * model->num_controls;
-	size_t counters_size = sizeof(struct op_msr) * model->num_counters;
 
-	int i;
 	for_each_online_cpu (i) {
-		cpu_msrs[i].counters = xmalloc_bytes(counters_size);
+		cpu_msrs[i].counters = xmalloc_array(struct op_msr,
+						     model->num_counters);
 		if (!cpu_msrs[i].counters) {
 			success = 0;
 			break;
 		}
-		cpu_msrs[i].controls = xmalloc_bytes(controls_size);
+		cpu_msrs[i].controls = xmalloc_array(struct op_msr,
+						     model->num_controls);
 		if (!cpu_msrs[i].controls) {
 			success = 0;
 			break;
#9 lore raw

PATCH 6/11 x86/IRQ: avoid over-alignment in alloc_pirq_struct()

In reply to Jan Beulich

In particular in the PV case xzalloc_bytes() forcing SMP_CACHE_BYTES alignment is counterproductive, as the allocation size there is only 40 bytes.

xen/arch/x86/irq.c +6 -3
@@ -1313,9 +1313,12 @@ void cleanup_domain_irq_mapping(struct d
 
 struct pirq *alloc_pirq_struct(struct domain *d)
 {
-    size_t sz = is_hvm_domain(d) ? sizeof(struct pirq) :
-                                   offsetof(struct pirq, arch.hvm);
-    struct pirq *pirq = xzalloc_bytes(sz);
+    union pirq_pv {
+        char space[offsetof(struct pirq, arch.hvm)];
+        void *align;
+    };
+    struct pirq *pirq = is_hvm_domain(d) ? xzalloc(struct pirq)
+                                         : (void *)xzalloc(union pirq_pv);
 
     if ( pirq )
     {
#10 lore raw

PATCH 7/11 EFI/runtime: avoid effectively open-coding xmalloc_array()

In reply to Jan Beulich

There is a difference in generated code: xmalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/common/efi/runtime.c +3 -3
@@ -486,7 +486,7 @@ int efi_runtime_call(struct xenpf_efi_ru
         size = op->u.get_variable.size;
         if ( size )
         {
-            data = xmalloc_bytes(size);
+            data = xmalloc_array(unsigned char, size);
             if ( !data )
             {
                 xfree(name);
@@ -536,7 +536,7 @@ int efi_runtime_call(struct xenpf_efi_ru
             return -EIO;
         }
 
-        data = xmalloc_bytes(op->u.set_variable.size);
+        data = xmalloc_array(unsigned char, op->u.set_variable.size);
         if ( !data )
             rc = -ENOMEM;
         else if ( copy_from_guest(data, op->u.set_variable.data,
@@ -571,7 +571,7 @@ int efi_runtime_call(struct xenpf_efi_ru
             return -EINVAL;
 
         size = op->u.get_next_variable_name.size;
-        name.raw = xzalloc_bytes(size);
+        name.raw = xzalloc_array(unsigned char, size);
         if ( !name.raw )
             return -ENOMEM;
         if ( copy_from_guest(name.raw, op->u.get_next_variable_name.name,
#11 lore raw

PATCH 8/11 hypfs: avoid effectively open-coding xzalloc_array()

In reply to Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

To avoid the need to add a cast, do away with the only forward-declared struct hypfs_dyndata.

xen/common/hypfs.c +5 -5
@@ -72,7 +72,7 @@ enum hypfs_lock_state {
     hypfs_write_locked
 };
 static DEFINE_PER_CPU(enum hypfs_lock_state, hypfs_locked);
-static DEFINE_PER_CPU(struct hypfs_dyndata *, hypfs_dyndata);
+static DEFINE_PER_CPU(void *, hypfs_dyndata);
 
 static DEFINE_PER_CPU(const struct hypfs_entry *, hypfs_last_node_entered);
 
@@ -160,19 +160,19 @@ static void node_exit_all(void)
 void *hypfs_alloc_dyndata(unsigned long size)
 {
     unsigned int cpu = smp_processor_id();
-    struct hypfs_dyndata **dyndata = &per_cpu(hypfs_dyndata, cpu);
+    void **dyndata = &per_cpu(hypfs_dyndata, cpu);
 
     ASSERT(per_cpu(hypfs_locked, cpu) != hypfs_unlocked);
     ASSERT(*dyndata == NULL);
 
-    *dyndata = xzalloc_bytes(size);
+    *dyndata = xzalloc_array(unsigned char, size);
 
     return *dyndata;
 }
 
 void *hypfs_get_dyndata(void)
 {
-    struct hypfs_dyndata *dyndata = this_cpu(hypfs_dyndata);
+    void *dyndata = this_cpu(hypfs_dyndata);
 
     ASSERT(dyndata);
 
@@ -181,7 +181,7 @@ void *hypfs_get_dyndata(void)
 
 void hypfs_free_dyndata(void)
 {
-    struct hypfs_dyndata **dyndata = &this_cpu(hypfs_dyndata);
+    void **dyndata = &this_cpu(hypfs_dyndata);
 
     XFREE(*dyndata);
 }
Reviewed-by on v1 8/11
#12 lore raw

Re: [PATCH 08/11] hypfs: avoid effectively open-coding xzalloc_array()

Quoted 8 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

To avoid the need to add a cast, do away with the only forward-declared struct hypfs_dyndata.

Juergen

#13 lore raw

PATCH 9/11 kexec: avoid effectively open-coding xzalloc_flex_struct()

In reply to Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/common/kexec.c +12 -6
@@ -463,7 +463,10 @@ static void * alloc_from_crash_heap(cons
 /* Allocate a crash note buffer for a newly onlined cpu. */
 static int kexec_init_cpu_notes(const unsigned long cpu)
 {
-    Elf_Note * note = NULL;
+    struct elf_notes {
+        Elf_Note first;
+        unsigned char more[];
+    } *notes = NULL;
     int ret = 0;
     int nr_bytes = 0;
 
@@ -477,7 +480,8 @@ static int kexec_init_cpu_notes(const un
 
     /* If we dont care about the position of allocation, malloc. */
     if ( low_crashinfo_mode == LOW_CRASHINFO_NONE )
-        note = xzalloc_bytes(nr_bytes);
+        notes = xzalloc_flex_struct(struct elf_notes, more,
+                                    nr_bytes - sizeof(notes->first));
 
     /* Protect the write into crash_notes[] with a spinlock, as this function
      * is on a hotplug path and a hypercall path. */
@@ -490,26 +494,28 @@ static int kexec_init_cpu_notes(const un
         spin_unlock(&crash_notes_lock);
         /* Always return ok, because whether we successfully allocated or not,
          * another CPU has successfully allocated. */
-        xfree(note);
+        xfree(notes);
     }
     else
     {
         /* If we care about memory possition, alloc from the crash heap,
          * also protected by the crash_notes_lock. */
         if ( low_crashinfo_mode > LOW_CRASHINFO_NONE )
-            note = alloc_from_crash_heap(nr_bytes);
+            notes = alloc_from_crash_heap(nr_bytes);
 
-        crash_notes[cpu].start = note;
+        crash_notes[cpu].start = &notes->first;
         crash_notes[cpu].size = nr_bytes;
         spin_unlock(&crash_notes_lock);
 
         /* If the allocation failed, and another CPU did not beat us, give
          * up with ENOMEM. */
-        if ( ! note )
+        if ( ! notes )
             ret = -ENOMEM;
         /* else all is good so lets set up the notes. */
         else
         {
+            Elf_Note *note = &notes->first;
+
             /* Set up CORE note. */
             setup_note(note, "CORE", NT_PRSTATUS, sizeof(ELF_Prstatus));
             note = ELFNOTE_NEXT(note);
#14 lore raw

Re: [PATCH 09/11] kexec: avoid effectively open-coding xzalloc_flex_struct()

Quoted 5 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

This honestly looks like a backwards step.  In particular, ...

Quoted 16 lines
xen/common/kexec.c +4 -1
@@ -463,7 +463,10 @@ static void * alloc_from_crash_heap(cons
 /* Allocate a crash note buffer for a newly onlined cpu. */
 static int kexec_init_cpu_notes(const unsigned long cpu)
 {
-    Elf_Note * note = NULL;
+    struct elf_notes {
+        Elf_Note first;
+        unsigned char more[];
+    } *notes = NULL;
     int ret = 0;
     int nr_bytes = 0;
 

xen/common/kexec.c static int kexec_init_cpu_notes(const un · line 484

Quoted 6 lines
xen/common/kexec.c +2 -1
@@ -477,7 +480,8 @@ static int kexec_init_cpu_notes(const un
 
     /* If we dont care about the position of allocation, malloc. */
     if ( low_crashinfo_mode == LOW_CRASHINFO_NONE )
-        note = xzalloc_bytes(nr_bytes);
+        notes = xzalloc_flex_struct(struct elf_notes, more,
+                                    nr_bytes - sizeof(notes->first));

... this is far more error prone than the code you replaced, seeing as there is now a chance that it can underflow.

~Andrew

#15 lore raw

Re: [PATCH 09/11] kexec: avoid effectively open-coding xzalloc_flex_struct()

Quoted 26 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

This honestly looks like a backwards step.  In particular, ...

Quoted 17 lines
xen/common/kexec.c +4 -1
@@ -463,7 +463,10 @@ static void * alloc_from_crash_heap(cons
 /* Allocate a crash note buffer for a newly onlined cpu. */
 static int kexec_init_cpu_notes(const unsigned long cpu)
 {
-    Elf_Note * note = NULL;
+    struct elf_notes {
+        Elf_Note first;
+        unsigned char more[];
+    } *notes = NULL;
     int ret = 0;
     int nr_bytes = 0;
 
@@ -477,7 +480,8 @@ static int kexec_init_cpu_notes(const un
 
Quoted 8 lines

xen/common/kexec.c static int kexec_init_cpu_notes(const un · line 484

Quoted 5 lines
xen/common/kexec.c +2 -1
     /* If we dont care about the position of allocation, malloc. */
     if ( low_crashinfo_mode == LOW_CRASHINFO_NONE )
-        note = xzalloc_bytes(nr_bytes);
+        notes = xzalloc_flex_struct(struct elf_notes, more,
+                                    nr_bytes - sizeof(notes->first));

... this is far more error prone than the code you replaced, seeing as there is now a chance that it can underflow.

You're kidding, aren't you? sizeof_cpu_notes() can't possibly return a value which would allow underflow here. There would be bigger issues than the underflow if any such happened, in particular with any theoretical underflow here simply resulting in the allocation to fail.

The original code is type-unsafe and requests more alignment than necessary. Besides objecting to my present way of addressing this, I would find it rather helpful if you could mention some kind of alternative you'd consider acceptable. As said in the cover letter, I think mid-term we ought to do away with xmalloc_bytes(). Hence some alternative would be needed here anyway.

Jan

#16 lore raw

PATCH 10/11 video/lfb: avoid effectively open-coding xzalloc_array()

In reply to Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/drivers/video/lfb.c +3 -2
@@ -147,8 +147,9 @@ int __init lfb_init(struct lfb_prop *lfb
 {
     lfb.lfbp = *lfbp;
 
-    lfb.lbuf = xmalloc_bytes(lfb.lfbp.bytes_per_line);
-    lfb.text_buf = xzalloc_bytes(lfb.lfbp.text_columns * lfb.lfbp.text_rows);
+    lfb.lbuf = xmalloc_array(unsigned char, lfb.lfbp.bytes_per_line);
+    lfb.text_buf = xzalloc_array(unsigned char,
+                                 lfb.lfbp.text_columns * lfb.lfbp.text_rows);
     lfb.line_len = xzalloc_array(unsigned int, lfb.lfbp.text_columns);
 
     if ( !lfb.lbuf || !lfb.text_buf || !lfb.line_len )
#17 lore raw

PATCH 11/11 Arm/optee: don't open-code xzalloc_flex_struct()

In reply to Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

xen/arch/arm/tee/optee.c +1 -2
@@ -529,8 +529,7 @@ static struct optee_shm_buf *allocate_op
     while ( unlikely(old != atomic_cmpxchg(&ctx->optee_shm_buf_pages,
                                            old, new)) );
 
-    optee_shm_buf = xzalloc_bytes(sizeof(struct optee_shm_buf) +
-                                  pages_cnt * sizeof(struct page *));
+    optee_shm_buf = xzalloc_flex_struct(struct optee_shm_buf, pages, pages_cnt);
     if ( !optee_shm_buf )
     {
         err_code = -ENOMEM;
#18 lore raw

Re: [PATCH 11/11] Arm/optee: don't open-code xzalloc_flex_struct()

Hi Jan,

Quoted 3 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

So I think moving to xmalloc_flex_struct() is a pretty good move. But I am actually a bit confused with the argument used.

Could you provide some details why you think forcing the array to be aligned to the maximum cache line supported (128 bytes on Arm) is wrong?

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [PATCH 11/11] Arm/optee: don't open-code xzalloc_flex_struct()

Quoted 10 lines · Julien Grall
Quoted 3 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

So I think moving to xmalloc_flex_struct() is a pretty good move. But I am actually a bit confused with the argument used.

Could you provide some details why you think forcing the array to be aligned to the maximum cache line supported (128 bytes on Arm) is wrong?

It is not "wrong" in that sense, but if this is intended it shouldn't be arranged via use of xmalloc_bytes(). As also pointed out in a similar discussion on another sub-thread, imo xmalloc_bytes(), being type-unsafe, should go away altogether mid-term. If individual callers have specific alignment requirements (which ought to be the exception), they should explicitly request the needed alignment. If architectures would prefer all allocations to have certain minimum alignment (e.g. to avoid cacheline sharing, which was Andrew's argument) or other "arrangement" (alignment by itself may not be that interesting due to the bhdr placed ahead of the allocation), it should be the allocator itself that provides for this, not individual callers.

Jan

#20 lore raw

Re: [PATCH 11/11] Arm/optee: don't open-code xzalloc_flex_struct()

Hi Jan,

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 3 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

Quoted 8 lines
Quoted 5 lines

So I think moving to xmalloc_flex_struct() is a pretty good move. But I am actually a bit confused with the argument used.

Could you provide some details why you think forcing the array to be aligned to the maximum cache line supported (128 bytes on Arm) is wrong?

It is not "wrong" in that sense, but if this is intended it shouldn't be arranged via use of xmalloc_bytes().

This is not very clear from the commit message (or even the cover letter). How about:

"
The current use xzalloc_bytes() in optee is nearly an open-coded version of xzalloc_flex_struct() which was introduced after the driver was merged.

The main difference is xzalloc_bytes() will also force the allocation to be SMP_CACHE_BYTES aligned and therefore avoid sharing the cache line.

While sharing the cache line can have an impact on the performance, this is also true for most of the other users of x*alloc_flex_struct(). So if we want to prevent sharing a cache line, it should be part of x*alloc_flex_struct().

In this case, we don't need stricter alignment than what the allocator does. So the call to xzalloc_bytes() is now replaced with a call xzalloc_flex_Struct(). "

Ideally, we want the same sort of the commit message in the other patches.

Quoted 3 lines

As also pointed out in a
similar discussion on another sub-thread, imo xmalloc_bytes(), being type-unsafe, should go away altogether mid-term.

And I will support dropping xmalloc_bytes().

Quoted 8 lines

If individual callers
have specific alignment requirements (which ought to be the exception), they should explicitly request the needed alignment. If architectures would prefer all allocations to have certain minimum alignment (e.g. to avoid cacheline sharing, which was Andrew's argument) or other "arrangement" (alignment by itself may not be that interesting due to the bhdr placed ahead of the allocation), it should be the allocator itself that provides for this, not individual callers.

And I agree that the allocator should do the alignment if this benefits every allocation.

Cheers,

Signature · 1 line
Julien Grall
#21 lore raw

Re: [PATCH 11/11] Arm/optee: don't open-code xzalloc_flex_struct()

Quoted 33 lines · Julien Grall

Hi Jan,

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 3 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

Quoted 8 lines
Quoted 5 lines

So I think moving to xmalloc_flex_struct() is a pretty good move. But I am actually a bit confused with the argument used.

Could you provide some details why you think forcing the array to be aligned to the maximum cache line supported (128 bytes on Arm) is wrong?

It is not "wrong" in that sense, but if this is intended it shouldn't be arranged via use of xmalloc_bytes().

This is not very clear from the commit message (or even the cover letter). How about:

"
The current use xzalloc_bytes() in optee is nearly an open-coded version of xzalloc_flex_struct() which was introduced after the driver was merged.

The main difference is xzalloc_bytes() will also force the allocation to be SMP_CACHE_BYTES aligned and therefore avoid sharing the cache line.

While sharing the cache line can have an impact on the performance, this is also true for most of the other users of x*alloc_flex_struct(). So if we want to prevent sharing a cache line, it should be part of x*alloc_flex_struct().

Quoted 4 lines

In this case, we don't need stricter alignment than what the allocator does. So the call to xzalloc_bytes() is now replaced with a call xzalloc_flex_Struct().
"

Well, okay, I've inserted a slightly edited version of this into the patch. But ...

Quoted 1 line

Ideally, we want the same sort of the commit message in the other patches.

... I disagree here: In particular because of the recurring pattern, I dislike repeated overly verbose descriptions. I could perhaps see them as being not seen as overly verbose when looking at every commit on its own (like would happen when someone tries to do some archaeology later on), but in the context of such a series this is potentially harmful: If almost a dozen patches have almost the same sufficiently verbose description, possible differences may not be paid attention to.

Plus, granted possibly controversial as well, I'm afraid I'm not happy to (repeatedly) state obvious facts. The abuse (in almost all cases) of x[mz]alloc_bytes() is, afaict, in no way attributed to the resulting higher alignment, but rather in the desire to get away easily without needing to think about using a type-safe flavor. This said I will admit that prior to the introduction of x[mz]alloc_flex_struct() I can see how alternatives could quickly have got quite ugly. And for the few (if any) users which actually care about this higher alignment, it should have been noted at the time of introducing the use that the specific need for certain alignment shouldn't be implied by using this function, but rather be made explicit. This is even more so as it's not written down anywhere that x[mz]alloc_bytes() guarantees a certain alignment (i.e. if the plan wasn't anyway to phase out its use, it should have been permissible to change the alignment it requests from the underlying implementation without first auditing all users).

Jan

#22 lore raw

Re: [PATCH 11/11] Arm/optee: don't open-code xzalloc_flex_struct()

Quoted 44 lines · Jan Beulich
Quoted 33 lines · Julien Grall

Hi Jan,

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 3 lines · Jan Beulich

There is a difference in generated code: xzalloc_bytes() forces SMP_CACHE_BYTES alignment. I think we not only don't need this here, but actually don't want it.

Quoted 8 lines
Quoted 5 lines

So I think moving to xmalloc_flex_struct() is a pretty good move. But I am actually a bit confused with the argument used.

Could you provide some details why you think forcing the array to be aligned to the maximum cache line supported (128 bytes on Arm) is wrong?

It is not "wrong" in that sense, but if this is intended it shouldn't be arranged via use of xmalloc_bytes().

This is not very clear from the commit message (or even the cover letter). How about:

"
The current use xzalloc_bytes() in optee is nearly an open-coded version of xzalloc_flex_struct() which was introduced after the driver was merged.

The main difference is xzalloc_bytes() will also force the allocation to be SMP_CACHE_BYTES aligned and therefore avoid sharing the cache line.

While sharing the cache line can have an impact on the performance, this is also true for most of the other users of x*alloc_flex_struct(). So if we want to prevent sharing a cache line, it should be part of x*alloc_flex_struct().

Quoted 4 lines

In this case, we don't need stricter alignment than what the allocator does. So the call to xzalloc_bytes() is now replaced with a call xzalloc_flex_Struct().
"

Well, okay, I've inserted a slightly edited version of this into the patch. But ...

Quoted 1 line

Ideally, we want the same sort of the commit message in the other patches.

Quoted 8 lines

... I disagree here: In particular because of the recurring pattern, I dislike repeated overly verbose descriptions. I could perhaps see them as being not seen as overly verbose when looking at every commit on its own (like would happen when someone tries to do some archaeology later on), but in the context of such a series this is potentially harmful: If almost a dozen patches have almost the same sufficiently verbose description, possible differences may not be paid attention to.

There are two part to take into account:

1) Reviewer
2) Future reader

For reviewer, I agree that the information is redundant. Although, it is not like the cover letter (in this case) contain more information about the reasoning... ;)

However, such information is not redundant for a future reader. Patches in a series may be applied separately, in a different order, title mangled...

Quoted 3 lines

Plus, granted possibly controversial as well, I'm afraid I'm not happy to (repeatedly) state obvious facts.

I should probably remind you that what's obvious for you may not be for the other.

In particular, writing "I think we only don't need this here, but actually don't want it" is very unhelpful because it can be interpreted differently (my original answer is an example).

It doesn't cost much to write clear commit message. But it will cost a lot for the future reader (or even reviewer) to figure out the original intention.

Quoted 8 lines

The abuse (in almost
all cases) of x[mz]alloc_bytes() is, afaict, in no way attributed to the resulting higher alignment, but rather in the desire to get away easily without needing to think about using a type-safe flavor. This said I will admit that prior to the introduction of x[mz]alloc_flex_struct() I can see how alternatives could quickly have got quite ugly. And for the few (if any) users which actually care about this higher alignment, it should have been

Quoted 8 lines

noted at the time of introducing the use that the specific need for certain alignment shouldn't be implied by using this function, but rather be made explicit. This is even more so as it's not written down anywhere that x[mz]alloc_bytes() guarantees a certain alignment (i.e. if the plan wasn't anyway to phase out its use, it should have been permissible to change the alignment it requests from the underlying implementation without first auditing all users).

I don't think it is really helpful to argue on whether the developper did it by lazyness/lack of helpers or else.

As I wrote, dropping xmalloc_bytes() is a good move. But we should documenting our reasoning rather than hoping that everyone know your "obvious facts".

Cheers,

Signature · 1 line
Julien Grall
#23 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

In the long run I think we want to do away with these type-unsafe interfaces, the more that they also request (typically) excess alignment. This series of entirely independent patches is eliminating the instances where it's relatively clear that they're not just "blob" allocations.

Quoted 7 lines

03: x86/MCE: avoid effectively open-coding xmalloc_array() 04: x86/HVM: avoid effectively open-coding xmalloc_array() 05: x86/oprofile: avoid effectively open-coding xmalloc_array() 06: x86/IRQ: avoid over-alignment in alloc_pirq_struct() 07: EFI/runtime: avoid effectively open-coding xmalloc_array() 08: hypfs: avoid effectively open-coding xzalloc_array() 10: video/lfb: avoid effectively open-coding xzalloc_array()

The flex conversions are fine, but I am unconvinced by argument for interchanging array() and bytes().

The cacheline size is 64 bytes, and the minimum allocation size is 16, plus a bhdr overhead of 32 bytes, so you're already at most of a cacheline for a nominally-zero sized allocation.

There can however be a severe penalty from cacheline sharing, which is why the bytes() form does have a minimum alignment.  There is one xmalloc heap shared across the entire system, so you've got no idea what might be sharing the same cache line for sub-line allocations.

We should not support sub-line allocations IMO.  The savings is a handful of bytes at best, and some horrible performance cliffs to avoid.  People running virtualisation are not going to be ram constrained to the order of a few bytes.

For small allocations which don't require specific alignment, then putting bhdr and the allocation in the same line is fine (if we don't do this already), but we shouldn't be in the position of having two bhdr's in the same cache line, even if there are plenty of single-byte allocations in the theoretical worst case.

~Andrew

#24 lore raw
Quoted 16 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich

In the long run I think we want to do away with these type-unsafe interfaces, the more that they also request (typically) excess alignment. This series of entirely independent patches is eliminating the instances where it's relatively clear that they're not just "blob" allocations.

Quoted 7 lines

03: x86/MCE: avoid effectively open-coding xmalloc_array() 04: x86/HVM: avoid effectively open-coding xmalloc_array() 05: x86/oprofile: avoid effectively open-coding xmalloc_array() 06: x86/IRQ: avoid over-alignment in alloc_pirq_struct() 07: EFI/runtime: avoid effectively open-coding xmalloc_array() 08: hypfs: avoid effectively open-coding xzalloc_array() 10: video/lfb: avoid effectively open-coding xzalloc_array()

Quoted 6 lines

The flex conversions are fine, but I am unconvinced by argument for interchanging array() and bytes().

The cacheline size is 64 bytes, and the minimum allocation size is 16, plus a bhdr overhead of 32 bytes, so you're already at most of a cacheline for a nominally-zero sized allocation.

But you're aware that the alignment x[mz]alloc_bytes() forces is 128 bytes? Plus, while sizeof(struct bhdr) is indeed 32, the overhead on allocated blocks is

#define BHDR_OVERHEAD (sizeof(struct bhdr) - MIN_BLOCK_SIZE)

i.e. 16 (i.e. the other half of the 32 is already the minimum block size of 16 that you also mention). IOW a cacheline sized block would yield 48 bytes of usable space. Specifically a meaningful change in the PV case from what patch 06 does, where we only need 40 bytes.

Quoted 4 lines

There can however be a severe penalty from cacheline sharing, which is why the bytes() form does have a minimum alignment.  There is one xmalloc heap shared across the entire system, so you've got no idea what might be sharing the same cache line for sub-line allocations.

This would call for distinguishing short-lived allocations (and ones to be used mainly from a single CPU) from long-lived ones having system wide use. I.e. a request to gain further allocation function flavors, when already the introduction of the one new xv[mz]alloc() has caused long-winded discussions with (so far) no real outcome.

Quoted 10 lines

We should not support sub-line allocations IMO.  The savings is a handful of bytes at best, and some horrible performance cliffs to avoid.  People running virtualisation are not going to be ram constrained to the order of a few bytes.

For small allocations which don't require specific alignment, then putting bhdr and the allocation in the same line is fine (if we don't do this already), but we shouldn't be in the position of having two bhdr's in the same cache line, even if there are plenty of single-byte allocations in the theoretical worst case.

That's a request to tweak allocator internals then, not an argument against the conversions done here.

Jan