Skip to contentSkip to conversation

early_malloc() introduced to ARM architecture

PATCH New Marek Vasut 2 participants 3 messages Jul 28, 2012 – Jul 29, 2012 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CAEB7QLAf3ha5wp50+2R7_vhMh7bEU8EtHx-QvoRz3fXy+AsJtQ@mail.gmail.com

#1 lore raw

PATCH 1/1 early_malloc() introduced to ARM architecture

Dear Tomas Hlavacek,

I think we should still mark early patches as RFC.

Quoted 3 lines

early_malloc() introduced to ARM architecture (it is a proof of concept). GD datastructure extended for holding early-heap. mem_malloc_init() in board_init_r() has been put up by few lines.

I take it was sent by the gmail webIF ? Like WD said, not a good idea, git send- email is really the way. it's actually simple, we can discuss that tomorrow.

Quoted 31 lines
---
 arch/arm/include/asm/global_data.h |    4 +-
 arch/arm/lib/board.c               |   32 +++++++++--
 common/Makefile                    |    1 +
 common/earlymalloc.c               |  102
++++++++++++++++++++++++++++++++++++
 include/common.h                   |    1 +
 include/earlymalloc.h              |   84 +++++++++++++++++++++++++++++
 lib/asm-offsets.c                  |    2 +-
 7 files changed, 220 insertions(+), 6 deletions(-)
 create mode 100644 common/earlymalloc.c
 create mode 100644 include/earlymalloc.h
arch/arm/include/asm/global_data.h +2 -1
index c3ff789..215212a 100644
@@ -30,7 +30,8 @@
  * global variables during system initialization (until we have set
  * up the memory controller so that we can use RAM).
  *
- * Keep it *SMALL* and remember to set GENERATED_GBL_DATA_SIZE >
 sizeof(gd_t)
+ * Keep it *SMALL* and remember to set GENERATED_GBL_DATA_SIZE >
+ *                                     sizeof(gd_t) + EARLY_HEAP_SIZE
  */
 

typedef struct global_data {

arch/arm/include/asm/global_data.h typedef struct global_data { · line 90

Quoted 4 lines
arch/arm/include/asm/global_data.h +1 -0
@@ -86,6 +87,7 @@ typedef    struct    global_data {
 #endif
 } gd_t;
 
+

Ok well ... you know, this will need cleanup. But for RFC, this is OK.

arch/arm/lib/board.c line 55

Quoted 12 lines
arch/arm/include/asm/global_data.h +0 -0
 /*
  * Global Data Flags
  */
arch/arm/lib/board.c +1 -0
index 500e216..81ee27b 100644
@@ -52,6 +52,7 @@
 #include <fdtdec.h>
 #include <post.h>
 #include <logbuff.h>
+#include <earlymalloc.h>

Do we need early_malloc.h at all? malloc.h won't cut it?

arch/arm/lib/board.c void board_init_f(ulong bootflag) · line 277

Quoted 7 lines
arch/arm/lib/board.c +1 -0
 #ifdef CONFIG_BITBANGMII
 #include <miiphy.h>
@@ -273,6 +274,9 @@ void board_init_f(ulong bootflag)
 
     memset((void *)gd, 0, sizeof(gd_t));
 
+    early_malloc_init();

So this basically flips a bit, do it the other way and you don't need it.

Quoted 31 lines
arch/arm/lib/board.c +11 -0
+    debug("Early malloc initialized.\n");
+
     gd->mon_len = _bss_end_ofs;
 #ifdef CONFIG_OF_EMBED
     /* Get a pointer to the FDT */
@@ -452,12 +469,23 @@ void board_init_r(gd_t *id, ulong dest_addr)
     ulong flash_size;
 #endif
 
+    gd_t *old_gd = gd;
+
     gd = id;
 
     gd->flags |= GD_FLG_RELOC;    /* tell others: relocation done */
 
     monitor_flash_len = _end_ofs;
 
+    /* The Malloc area is immediately below the monitor copy in DRAM */
+    malloc_start = dest_addr - TOTAL_MALLOC_LEN;
+    mem_malloc_init (malloc_start, TOTAL_MALLOC_LEN);
+
+    // TODO: DM Cores relocation goes here.
+
+    /* Disable early mallocator (effectively switch calls to the real

malloc). */

arch/arm/lib/board.c +2 -0
+    early_malloc_disab((gde_t *)old_gd);
+
     /* Enable caches */
     enable_caches();

arch/arm/lib/board.c void board_init_r(gd_t *id, ulong dest_addr) · line 491

Quoted 7 lines
arch/arm/lib/board.c +0 -4
@@ -485,10 +513,6 @@ void board_init_r(gd_t *id, ulong dest_addr)
     post_output_backlog();
 #endif
 
-    /* The Malloc area is immediately below the monitor copy in DRAM */
-    malloc_start = dest_addr - TOTAL_MALLOC_LEN;
-    mem_malloc_init (malloc_start, TOTAL_MALLOC_LEN);
-

Can mem_malloc_init() not flip on the early_malloc bit for you here?

Quoted 3 lines
arch/arm/lib/board.c +0 -0
 #if !defined(CONFIG_SYS_NO_FLASH)
     puts("Flash: ");
 

[...]

Quoted 18 lines
arch/arm/lib/board.c +18 -0
+
+#include <asm/global_data.h> /* for gd_t and gd */
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static inline size_t early_malloc_align_up(phys_addr_t addr)
+{
+  if(!EARLY_MALLOC_IS_ALIGNED(addr)) {
+    addr = EARLY_MALLOC_ALIGN_DOWN(addr);
+    addr += EARLY_MALLOC_ALIGN_MULTIPLE;
+  }
+
+  return addr;
+}
+
+void early_malloc_init(void)
+{
+  ((gde_t *)gd)->em.flags = EARLY_MALLOC_INIT_FLAGS;

arch/arm/lib/board.c

Quoted 10 lines
arch/arm/lib/board.c +10 -0
+  ((gde_t *)gd)->em.size = 0;
+}
+
+
+int early_malloc_isactive(void)
+{
+  /* The early_malloc() is inactive after relocation or when the
+     flag says so. */
+  return ((EARLY_MALLOC_IS_ACTIVE(((gde_t *)gd)->em.flags)) ||
+      (gd->flags & GD_FLG_RELOC));

So ... gd->flags will be enough for the early malloc, you don't need any further flags

arch/arm/lib/board.c

Quoted 10 lines
arch/arm/lib/board.c +10 -0
+}
+
+int early_free_isactive(void)
+{
+  /* The early free is inactive when the flag says so. */
+  return (gd->flags & GD_FLG_RELOC);
+}
+
+
+void *early_malloc(size_t size)

No, we want this wrapped into malloc() call, so the drivers can be inited indifferent of time.

arch/arm/lib/board.c

Quoted 6 lines
arch/arm/lib/board.c +6 -0
+{
+  em_spc_t *em = &((gde_t *)gd)->em;
+
+  /* Check flag active. */
+  if(!EARLY_MALLOC_IS_ACTIVE(em->flags))
+    return NULL;

Indent with tab please ... tools/checkpatch.pl will help here.

Quoted 19 lines
arch/arm/lib/board.c +2 -0
+  /* Choose block beginning address. */
+  phys_addr_t addr =

early_malloc_align_up(((phys_addr_t)&em->heap)+em->size);

arch/arm/lib/board.c +16 -0
+
+  /* Check free space. */
+  if(addr + size >= ((phys_addr_t)(&(em->heap))) + EARLY_HEAP_SIZE)
+    return NULL;
+
+  /* Calculate next free space. */
+  em->size+=early_malloc_align_up(size);
+
+  return (void *)addr;
+}
+
+
+int early_malloc_isaddress(void *addr)
+{
+  if((((phys_addr_t)addr) >= (phys_addr_t)(&(((gde_t *)gd)->em.heap)))&&
+     (((phys_addr_t)addr) < ((phys_addr_t)(&(((gde_t
Quoted 1 line

*)gd)->em.heap)))+EARLY_HEAP_SIZE))

See container_of() ... and you can really split the checks.

arch/arm/lib/board.c

Quoted 8 lines
arch/arm/lib/board.c +8 -0
+    return 1;
+  else
+    return 0;
+}
+
+void early_free(void *addr)
+{
+  /* NOOP - Early malloc does not support free(). */

This goes to free() call ...

Quoted 16 lines
arch/arm/lib/board.c +6 -0
+}
+
+void early_malloc_disab(gde_t *gd)
+{
+  EARLY_MALLOC_DISABLE(gd->em.flags);
+}
include/common.h +1 -0
index a2c6b27..486925f 100644
@@ -171,6 +171,7 @@ typedef void (interrupt_handler_t)(void *);
 
 #include <asm/u-boot.h> /* boot information for Linux kernel */
 #include <asm/global_data.h>    /* global data used for startup functions
 */
+#include <earlymalloc.h> /* Early mallocator data structure that extends
Quoted 1 line

global data. */

Drop this altogether.
[...]

include/common.h

Quoted 6 lines
include/common.h +6 -0
+
+typedef struct early_malloc_spc {
+        char            flags;
+        short           size;
+        char            heap[EARLY_HEAP_SIZE];
+} em_spc_t;

Drop the typedef please.

include/common.h

Quoted 11 lines
include/common.h +11 -0
+/*
+ * Platform data extended for the early mallocator.
+ * This structure holds the original global data (and it starts at
+ * the very same address), but it is longer because it holds
+ * early malloc space in the end.
+ * This is a trick with data structures: This datastructure is
+ * pre-allocated by lib/asm-offsets.c but this structure is not to
+ * be relocated as whole because struct global_data is used during
+ * normal operations as well as during relocation phase.
+ */
+typedef struct global_data_extended {

I'm not convinced about the typedef here either ... others?

Quoted 21 lines

+ gd_t gd; /* Original Global Data
structure */
+ em_spc_t em; /* Early-malloc space.
*/

include/common.h +11 -0
+} gde_t;
+
+/* Initial flags block. Active flag set. */
+#define EARLY_MALLOC_INIT_FLAGS 0x80
+
+/* Alignment size - number of bytes to align to it's mupliple. */
+#define EARLY_MALLOC_ALIGN_MULTIPLE (sizeof(phys_addr_t))
+#define EARLY_MALLOC_ALIGN_MASK (EARLY_MALLOC_ALIGN_MULTIPLE-1)
+
+
+#define EARLY_MALLOC_ALIGN_DOWN(addr) ((phys_addr_t)addr &

(~EARLY_MALLOC_ALIGN_MASK))
+#define EARLY_MALLOC_IS_ALIGNED(addr) (!(((phys_addr_t)addr) & EARLY_MALLOC_ALIGN_MASK))

include/common.h +3 -0
+#define EARLY_MALLOC_FLAG_ACTIVE 0x80
+#define EARLY_MALLOC_IS_ACTIVE(flags) (flags & EARLY_MALLOC_FLAG_ACTIVE)
+#define EARLY_MALLOC_DISABLE(flags)
Quoted 1 line

(flags=(flags&(~EARLY_MALLOC_FLAG_ACTIVE)))

You won't need this whole file I think ...

Quoted 16 lines
include/common.h +11 -0
+
+
+void early_malloc_init(void);
+int early_malloc_isactive(void);
+int early_free_isactive(void);
+void *early_malloc(size_t size);
+void early_free(void *addr);
+void early_malloc_disab(gde_t *gd);
+int early_malloc_isaddress(void *);
+
+#endif
lib/asm-offsets.c +0 -0
index c88f5d4..6e39826 100644

lib/asm-offsets.c int main(void) · line 26

Quoted 5 lines
lib/asm-offsets.c +1 -1
@@ -23,7 +23,7 @@ int main(void)
 {
     /* Round up to make sure size gives nice stack alignment */
     DEFINE(GENERATED_GBL_DATA_SIZE,
-        (sizeof(struct global_data) + 15) & ~15);
+        (sizeof(struct global_data_extended) + 15) & ~15);

And if you adjusted the global data to be the "container of both, you won't need this here either.

Quoted 3 lines
lib/asm-offsets.c +0 -0
     DEFINE(GENERATED_BD_INFO_SIZE,
         (sizeof(struct bd_info) + 15) & ~15);
#2 lore raw

PATCH 1/1 early_malloc() introduced to ARM architecture

Hello Marek,

Quoted 1 line · Marek Vasut

I think we should still mark early patches as RFC.

Sure...

Quoted 3 lines
Quoted 1 line

+#include <earlymalloc.h>

Do we need early_malloc.h at all? malloc.h won't cut it?

My intention was to keep the early_malloc in separate instance from real malloc. Although this is proof-of-concept / RFC for early_malloc, it does not contain adaptation layer for switching malloc/early_malloc, because it seriously break things. I wanted comments on the basic idea first.

Quoted 2 lines

+ early_malloc_init();
So this basically flips a bit, do it the other way and you don't need it.

Well yes. I was told to support more stages of early_mallocator deactivation during DM core relocation. So this is why I have plenty of flags (currently using one). Now I know at least about two stages and three states. Actually intended lifecycle of the early_malloc is:

1) init: malloc() calls are switched to early_malloc(), other calls (= free(), realloc()...) are switched to dummy.
2) relocation done, real malloc initialized, before core relocation:
malloc() switched to dlmalloc, other calls to dummy (this is special mode for core relocation, which prevents calling dlmaloc free() on space allocated by early_malloc().
3) Full switch to dlmalloc.

Sure we can make it simpler, if it is OK with prospective core relocation function.

Quoted 3 lines

No, we want this wrapped into malloc() call, so the drivers can be inited indifferent of time.

Sure. My intention was not to hack current malloc but rather create adaptation layer. Maybe not a good idea... Anyway I can put simple switches like

if(!(gd->flags & GD_FLG_RELOC))

	return early_malloc();

directly into malloc() functions.

Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+
+typedef struct early_malloc_spc {
+        char            flags;
+        short           size;
+        char            heap[EARLY_HEAP_SIZE];
+} em_spc_t;

Drop the typedef please.

Actually I don't follow what is the problem here with the typedef?

Quoted 6 lines
Quoted 6 lines
lib/asm-offsets.c +0 -0
index c88f5d4..6e39826 100644
@@ -23,7 +23,7 @@ int main(void)
 {
Quoted 8 lines

lib/asm-offsets.c int main(void) · line 26

Quoted 4 lines
lib/asm-offsets.c +1 -1
     /* Round up to make sure size gives nice stack alignment */
     DEFINE(GENERATED_GBL_DATA_SIZE,
-        (sizeof(struct global_data) + 15) & ~15);
+        (sizeof(struct global_data_extended) + 15) & ~15);

And if you adjusted the global data to be the "container of both, you won't need
this here either.

Sure. My intention was to keep early_malloc heap in separate structure (containing the GD structure) in order not to relocate it but rather relocate cores by ourselves, which was our plan. It would be simpler to have it in the end of standard GD structure, because we can drop multi-stage early_malloc turn-off and perhaps we are not going to need to do relocation of DM structures by hand.

Tomas

--
Tom?? Hlav??ek <[email protected]>

#3 lore raw

PATCH 1/1 early_malloc() introduced to ARM architecture

Dear Tomas Hlavacek,

Quoted 5 lines

Hello Marek,

Quoted 1 line · Marek Vasut

I think we should still mark early patches as RFC.

Quoted 8 lines

Sure...

Quoted 3 lines
Quoted 1 line

+#include <earlymalloc.h>

Do we need early_malloc.h at all? malloc.h won't cut it?

My intention was to keep the early_malloc in separate instance from real malloc.

early_malloc.c is probably fine ... once you manage to properly wrap the calls to early_malloc()/dlmalloc() into malloc() and switch based on the GD flags. It's just the header file that I consider redundant.

Quoted 3 lines

Although this is proof-of-concept / RFC for early_malloc, it does not contain adaptation layer for switching malloc/early_malloc, because it seriously break things.

Why/how?

Quoted 8 lines

I wanted
comments on the basic idea first.

Quoted 2 lines

+ early_malloc_init();
So this basically flips a bit, do it the other way and you don't need it.

Well yes. I was told to support more stages of early_mallocator deactivation during DM core relocation.

Ok, add more flags maybe? Can you elaborate on the reason?

Quoted 2 lines

So this is why I have plenty
of flags (currently using one).

Still, can you not wrap it into GD flags ?

Quoted 4 lines

Now I know at least about two stages
and three states. Actually intended lifecycle of the early_malloc is:

1) init:

What's "init" ... you mean board_init_f() ?

Quoted 1 line

malloc() calls are switched to early_malloc()

Ok, but you must wrap this into malloc() call

Quoted 1 line

other calls (=free(), realloc()...) are switched to dummy.

realloc() can work too, why not ? So can calloc() etc.

Quoted 1 line

2) relocation done

board_init_r() ?

Quoted 5 lines

real malloc initialized, before core relocation:
malloc() switched to dlmalloc
other calls to dummy (this is special
mode for core relocation, which prevents calling dlmaloc free() on space allocated by early_malloc().

I see ... ok, se please check if the GD flags can't be used, I think they can.

Quoted 8 lines

3) Full switch to dlmalloc.

Sure we can make it simpler, if it is OK with prospective core relocation function.

Quoted 2 lines

No, we want this wrapped into malloc() call, so the drivers can be inited indifferent of time.

Quoted 8 lines

Sure. My intention was not to hack current malloc but rather create adaptation layer. Maybe not a good idea... Anyway I can put simple switches like

if(!(gd->flags & GD_FLG_RELOC))

	return early_malloc();

directly into malloc() functions.

You can actually switch the mallocator so the functions exported by it are prefixed with dl ... and then just create some stuff that's called malloc() free() etc. and does the switching.

Quoted 10 lines
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+typedef struct early_malloc_spc {
+        char            flags;
+        short           size;
+        char            heap[EARLY_HEAP_SIZE];
+} em_spc_t;

Drop the typedef please.

Actually I don't follow what is the problem here with the typedef?

http://lwn.net/Articles/2241/ ... but I'd like to gain opinions from other people on this one.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
lib/asm-offsets.c +0 -0
index c88f5d4..6e39826 100644
@@ -23,7 +23,7 @@ int main(void)
 
 {
 
     /* Round up to make sure size gives nice stack alignment */
     DEFINE(GENERATED_GBL_DATA_SIZE,
 
Quoted 8 lines
Quoted 6 lines

lib/asm-offsets.c int main(void) · line 29

Quoted 2 lines
lib/asm-offsets.c +1 -1
-        (sizeof(struct global_data) + 15) & ~15);
+        (sizeof(struct global_data_extended) + 15) & ~15);

And if you adjusted the global data to be the "container of both, you won't need
this here either.

Sure. My intention was to keep early_malloc heap in separate structure

Of course. My point was to do it the other way. But looking at it one more time, this seems correct.

Quoted 11 lines

(containing the GD structure) in order not to relocate it but rather relocate cores by ourselves, which was our plan. It would be simpler to have it in the end of standard GD structure, because we can drop multi-stage early_malloc turn-off and perhaps we are not going to need to do relocation of DM structures by hand.

Tomas

--
Tom?? Hlav??ek <[email protected]>

Best regards,
Marek Vasut