lib: sysmem: optimise alloc policy

- Allow request region alloc within the first invisiable region reserved by
  bidram when request region has flags M_ATTR_IGNORE_INVISIBLE. This is a
  workaround for some firmware memory layout, eg: on RK3308-AArch32, the ATF
  region is 0~1M(same as RK3308-AArch64), but the kernel would like to alloc
  at 0x00058000.

- Always make kernel reserved-memory alloc successfully and check overlap
  with invisible and sysmem allocated regions in sysmem_overflow_check()
  before bootm. This makes alloc policy more easier.

Change-Id: I533c710a6e69bd930befda441b9ec64415e3f408
Signed-off-by: Joseph Chen <chenjh@rock-chips.com>
This commit is contained in:
Joseph Chen 2019-07-15 14:17:51 +08:00
parent fc7ff0f015
commit 50226c8f95
3 changed files with 204 additions and 94 deletions

View File

@ -6,6 +6,8 @@
#ifndef _MEMBLK_H #ifndef _MEMBLK_H
#define _MEMBLK_H #define _MEMBLK_H
#define ALIAS_COUNT_MAX 2
enum memblk_id { enum memblk_id {
MEMBLK_ID_UNK, MEMBLK_ID_UNK,
@ -31,20 +33,21 @@ enum memblk_id {
/* Other */ /* Other */
MEMBLK_ID_BY_NAME, MEMBLK_ID_BY_NAME,
MEMBLK_ID_FDT_RESV, MEMBLK_ID_KMEM_RESERVED,
MEMBLK_ID_DEMO, MEMBLK_ID_DEMO,
MEMBLK_ID_MAX, MEMBLK_ID_MAX,
}; };
struct memblk_attr { struct memblk_attr {
const char *name; const char *name;
const char *alias[2]; const char *alias[ALIAS_COUNT_MAX];
u32 flags; u32 flags;
}; };
struct memblock { struct memblock {
phys_addr_t base; phys_addr_t base;
phys_size_t size; phys_size_t size;
phys_addr_t orig_base;
struct memblk_attr attr; struct memblk_attr attr;
struct list_head node; struct list_head node;
}; };
@ -59,11 +62,18 @@ extern const struct memblk_attr *mem_attr;
#define M_ATTR_OFC (1 << 0) #define M_ATTR_OFC (1 << 0)
/* Over-Flow-Check for region Head, only for U-Boot stack */ /* Over-Flow-Check for region Head, only for U-Boot stack */
#define M_ATTR_HOFC (1 << 1) #define M_ATTR_HOFC (1 << 1)
/* Memory can be overlap by fdt reserved memory */ /* Memory can be overlap by fdt reserved memory, deprecated */
#define M_ATTR_OVERLAP (1 << 2) #define M_ATTR_OVERLAP (1 << 2)
/* Just peek, always return success */ /* Just peek, always return success, deprecated */
#define M_ATTR_PEEK (1 << 3) #define M_ATTR_PEEK (1 << 3)
/* The region start address should be aligned to cacheline size */ /* The region start address should be aligned to cacheline size */
#define M_ATTR_CACHELINE_ALIGN (1 << 4) #define M_ATTR_CACHELINE_ALIGN (1 << 4)
/* Kernel 'reserved-memory' */
#define M_ATTR_KMEM_RESERVED (1 << 5)
/* The region can be overlap by kernel 'reserved-memory' */
#define M_ATTR_KMEM_CAN_OVERLAP (1 << 6)
/* Ignore invisable region reserved by bidram */
#define M_ATTR_IGNORE_INVISIBLE (1 << 7)
#endif /* _MEMBLK_H */ #endif /* _MEMBLK_H */

View File

@ -20,7 +20,9 @@
struct sysmem { struct sysmem {
struct lmb lmb; struct lmb lmb;
struct list_head allocated_head; struct list_head allocated_head;
struct list_head kmem_resv_head;
ulong allocated_cnt; ulong allocated_cnt;
ulong kmem_resv_cnt;
bool has_initf; bool has_initf;
bool has_initr; bool has_initr;
}; };

View File

@ -4,6 +4,7 @@
*/ */
#include <common.h> #include <common.h>
#include <bidram.h>
#include <sysmem.h> #include <sysmem.h>
#include <lmb.h> #include <lmb.h>
#include <malloc.h> #include <malloc.h>
@ -38,6 +39,22 @@ bool sysmem_has_init(void)
plat_sysmem.has_initr : plat_sysmem.has_initf; plat_sysmem.has_initr : plat_sysmem.has_initf;
} }
static inline int sysmem_is_overlap(phys_addr_t base1, phys_size_t size1,
phys_addr_t base2, phys_size_t size2)
{
return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
}
static inline int sysmem_is_sub_region(struct memblock *sub,
struct memblock *main)
{
if (!sub || !main)
return false;
return ((sub->base >= main->base) &&
(sub->base + sub->size <= main->base + main->size));
}
void sysmem_dump(void) void sysmem_dump(void)
{ {
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
@ -89,16 +106,37 @@ void sysmem_dump(void)
overflow = false; overflow = false;
} }
printf(" allocated.rgn[%ld].name = \"%s\" %s\n", printf(" allocated.rgn[%ld].name = \"%s\" %s %s\n",
i, mem->attr.name, overflow ? " <Overflow!>" : ""); i, mem->attr.name, overflow ? " <Overflow!>" : "",
mem->orig_base != mem->base ? "<*>" : "");
printf(" .addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", printf(" .addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n",
(ulong)mem->base, (ulong)(mem->base + mem->size), (ulong)mem->orig_base,
(ulong)(mem->orig_base + mem->size),
(ulong)mem->size); (ulong)mem->size);
i++; i++;
} }
printf("\n malloc_r: %d MiB, malloc_f: %d KiB\n", /* Kernel 'reserved-memory' */
SIZE_MB(CONFIG_SYS_MALLOC_LEN), SIZE_KB(CONFIG_SYS_MALLOC_F_LEN)); i = 0;
printf("\n");
list_for_each(node, &sysmem->kmem_resv_head) {
mem = list_entry(node, struct memblock, node);
allocated_size += mem->size;
printf(" kmem-resv.rgn[%ld].name = \"%s\" %s\n",
i, mem->attr.name,
mem->orig_base != mem->base ? "<*>" : "");
printf(" .addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n",
(ulong)mem->orig_base,
(ulong)(mem->orig_base + mem->size),
(ulong)mem->size);
i++;
}
printf("\n framework malloc_r = %3d MiB",
SIZE_MB(CONFIG_SYS_MALLOC_LEN));
printf("\n framework malloc_f = %3d KiB\n",
SIZE_KB(CONFIG_SYS_MALLOC_F_LEN));
printf("\n allocated.total = 0x%08lx (%ld MiB. %ld KiB)\n", printf("\n allocated.total = 0x%08lx (%ld MiB. %ld KiB)\n",
(ulong)allocated_size, (ulong)allocated_size,
SIZE_MB((ulong)allocated_size), SIZE_MB((ulong)allocated_size),
@ -109,7 +147,7 @@ void sysmem_dump(void)
reserved_size = 0; reserved_size = 0;
for (i = 0; i < lmb->reserved.cnt; i++) { for (i = 0; i < lmb->reserved.cnt; i++) {
reserved_size += lmb->reserved.region[i].size; reserved_size += lmb->reserved.region[i].size;
printf(" LMB.reserved[%ld].addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", i, printf(" LMB.allocated[%ld].addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", i,
(ulong)lmb->reserved.region[i].base, (ulong)lmb->reserved.region[i].base,
(ulong)lmb->reserved.region[i].base + (ulong)lmb->reserved.region[i].base +
(ulong)lmb->reserved.region[i].size, (ulong)lmb->reserved.region[i].size,
@ -126,42 +164,103 @@ void sysmem_dump(void)
void sysmem_overflow_check(void) void sysmem_overflow_check(void)
{ {
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
struct list_head *node; struct list_head *node, *knode;
struct memcheck *check; struct memcheck *check;
struct memblock *mem; struct memblock *kmem;
int overflow; struct memblock *smem;
struct memblock *rmem;
int overflow = 0, overlap = 0;
if (!sysmem_has_init()) if (!sysmem_has_init())
return; return;
#ifdef CONFIG_BIDRAM
/*
* Check kernel 'reserved-memory' overlap with invisible regions
*
* Here, only print warning message when overlap with invisible region
*/
list_for_each(knode, &sysmem->kmem_resv_head) {
kmem = list_entry(knode, struct memblock, node);
rmem = bidram_reserved_is_overlap(kmem->base, kmem->size);
if (rmem) {
const char *alias;
int i, dump = 1;
/*
* Ignore the sub region of invisible region.
* eg: ramoops of SHM.
*/
alias = rmem->attr.alias[0];
if (alias && sysmem_is_sub_region(kmem, rmem)) {
for (i = 0; i < ALIAS_COUNT_MAX; i++, alias++) {
alias = rmem->attr.alias[i];
if (!alias)
continue;
if (!strncasecmp(kmem->attr.name, alias,
strlen(alias))) {
dump = 0;
break;
}
}
}
if (dump)
SYSMEM_W("kernel 'reserved-memory' \"%s\"(0x%08lx - 0x%08lx) "
"is overlap with [invisible] \"%s\" (0x%08lx - 0x%08lx)\n",
kmem->attr.name, (ulong)kmem->base,
(ulong)(kmem->base + kmem->size),
rmem->attr.name, (ulong)rmem->base,
(ulong)(rmem->base + rmem->size));
}
}
#endif
list_for_each(node, &sysmem->allocated_head) { list_for_each(node, &sysmem->allocated_head) {
mem = list_entry(node, struct memblock, node); smem = list_entry(node, struct memblock, node);
if (mem->attr.flags & M_ATTR_OFC) { /*
* Check kernel 'reserved-memory' overlap with sysmem allocated regions
*/
list_for_each(knode, &sysmem->kmem_resv_head) {
kmem = list_entry(knode, struct memblock, node);
if (sysmem_is_overlap(smem->base, smem->size,
kmem->base, kmem->size)) {
if (smem->attr.flags & M_ATTR_KMEM_CAN_OVERLAP)
continue;
overlap = 1;
SYSMEM_W("kernel 'reserved-memory' \"%s\"(0x%08lx - 0x%08lx) "
"is overlap with \"%s\" (0x%08lx - 0x%08lx)\n",
kmem->attr.name, (ulong)kmem->base,
(ulong)(kmem->base + kmem->size),
smem->attr.name, (ulong)smem->base,
(ulong)(smem->base + smem->size));
}
}
/*
* Check sysmem allocated regions overflow.
*/
if (smem->attr.flags & M_ATTR_OFC) {
check = (struct memcheck *) check = (struct memcheck *)
(mem->base + mem->size - sizeof(*check)); (smem->base + smem->size - sizeof(*check));
overflow = (check->magic != SYSMEM_MAGIC); overflow = (check->magic != SYSMEM_MAGIC);
} else if (mem->attr.flags & M_ATTR_HOFC) { } else if (smem->attr.flags & M_ATTR_HOFC) {
check = (struct memcheck *) check = (struct memcheck *)
(mem->base - sizeof(*check)); (smem->base - sizeof(*check));
overflow = (check->magic != SYSMEM_MAGIC); overflow = (check->magic != SYSMEM_MAGIC);
} else { } else {
overflow = 0; overflow = 0;
} }
if (overflow) if (overflow) {
SYSMEM_E("Found there is region overflow!\n");
break; break;
}
} }
if (overflow) { if (overflow || overlap)
SYSMEM_E("Found there is region overflow!\n");
sysmem_dump(); sysmem_dump();
}
}
static inline int sysmem_is_overlap(phys_addr_t base1, phys_size_t size1,
phys_addr_t base2, phys_size_t size2)
{
return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
} }
static int sysmem_add(phys_addr_t base, phys_size_t size) static int sysmem_add(phys_addr_t base, phys_size_t size)
@ -183,7 +282,7 @@ static int sysmem_add(phys_addr_t base, phys_size_t size)
static const char *sysmem_alias2name(const char *name, int *id) static const char *sysmem_alias2name(const char *name, int *id)
{ {
const char *alias; const char *alias;
int n, i, j; int i, j;
int match = 0; int match = 0;
for (i = 0; i < MEMBLK_ID_MAX; i++) { for (i = 0; i < MEMBLK_ID_MAX; i++) {
@ -198,8 +297,8 @@ static const char *sysmem_alias2name(const char *name, int *id)
if (!alias) if (!alias)
continue; continue;
n = ARRAY_SIZE(mem_attr[i].alias); for (j = 0; j < ALIAS_COUNT_MAX; j++) {
for (j = 0; j < n; j++, alias++) { alias = mem_attr[i].alias[j];
if (alias && !strcasecmp(alias, name)) { if (alias && !strcasecmp(alias, name)) {
match = 1; match = 1;
goto finish; goto finish;
@ -231,59 +330,50 @@ static void *sysmem_alloc_align_base(enum memblk_id id,
phys_addr_t paddr; phys_addr_t paddr;
phys_addr_t alloc_base; phys_addr_t alloc_base;
phys_size_t alloc_size; phys_size_t alloc_size;
phys_addr_t bank_base; phys_addr_t orig_base = base;
phys_size_t bank_size;
bool req_overlap = false; /* Only for kernel reserved-memory */
int i;
if (!sysmem_has_init()) if (!sysmem_has_init())
goto out; goto out;
if (id == MEMBLK_ID_BY_NAME || id == MEMBLK_ID_FDT_RESV) { if (id == MEMBLK_ID_BY_NAME || id == MEMBLK_ID_KMEM_RESERVED) {
if (!mem_name) { if (!mem_name) {
SYSMEM_E("NULL name for alloc sysmem\n"); SYSMEM_E("NULL name for alloc sysmem\n");
goto out; goto out;
} else if (id == MEMBLK_ID_FDT_RESV) {
/*
* Allow fdt reserved memory to overlap with the region
* only used in U-Boot, like: stack, fastboot, u-boot...
* these regions are marked as M_ATTR_OVERLAP in flags.
*
* Here we check whether it overlaps with others, if
* so, set req_overlap as true.
*/
for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
if (!gd->bd->bi_dram[i].size)
continue;
bank_base = gd->bd->bi_dram[i].start;
bank_size = gd->bd->bi_dram[i].size;
if (sysmem_is_overlap(base, size,
bank_base, bank_size)) {
req_overlap = true;
break;
}
}
/*
* If this request region is out size of all available
* region, ignore and return success.
*/
if (!req_overlap)
return (void *)base;
} }
/* Find name, id and attr by outer mem_name */ /* Find: name, id and attr by outer mem_name & id */
name = sysmem_alias2name(mem_name, (int *)&id); name = sysmem_alias2name(mem_name, (int *)&id);
attr = mem_attr[id]; attr = mem_attr[id];
if (!attr.name) if (!attr.name)
attr.name = strdup(name); attr.name = strdup(name);
/* Always make kernel 'reserved-memory' alloc successfully */
if (id == MEMBLK_ID_KMEM_RESERVED) {
struct memblock *mem;
mem = malloc(sizeof(*mem));
if (!mem) {
SYSMEM_E("No memory for \"%s\" alloc sysmem\n", name);
return mem;
}
attr.flags |= M_ATTR_KMEM_RESERVED;
mem->orig_base = orig_base;
mem->base = base;
mem->size = size;
mem->attr = attr;
sysmem->kmem_resv_cnt++;
list_add_tail(&mem->node, &sysmem->kmem_resv_head);
return (void *)base;
}
} else if (id > MEMBLK_ID_UNK && id < MEMBLK_ID_MAX) { } else if (id > MEMBLK_ID_UNK && id < MEMBLK_ID_MAX) {
attr = mem_attr[id]; attr = mem_attr[id];
name = attr.name; name = attr.name;
/* /*
* Special handle for Android AVB alloc(on any where)
*
* Fixup base and place right after U-Boot stack, adding a lot * Fixup base and place right after U-Boot stack, adding a lot
* of space(4KB) maybe safer. * of space(4KB) maybe safer.
*/ */
@ -291,15 +381,25 @@ static void *sysmem_alloc_align_base(enum memblk_id id,
(base == SYSMEM_ALLOC_ANYWHERE)) { (base == SYSMEM_ALLOC_ANYWHERE)) {
base = gd->start_addr_sp - base = gd->start_addr_sp -
CONFIG_SYS_STACK_SIZE - size - 0x1000; CONFIG_SYS_STACK_SIZE - size - 0x1000;
/*
* So far, we use M_ATTR_PEEK for uncompress kernel alloc, and } else if (base <= gd->bd->bi_dram[0].start) {
* for ARMv8 enabling AArch32 mode, the ATF is still AArch64 /*
* and ocuppies 0~1MB and shmem 1~2M. So let's ignore the region * On Rockchip platform:
* which overlap with them. *
*/ * So far, we use M_ATTR_IGNORE_INVISIBLE for uncompress
} else if (attr.flags & M_ATTR_PEEK) { * kernel alloc, and for ARMv8 enabling AArch32 mode, the
if (base <= gd->bd->bi_dram[0].start) * ATF is still AArch64 and ocuppies 0~1MB and shmem 1~2M.
* So let's ignore the region which overlap with them.
*/
if (attr.flags & M_ATTR_IGNORE_INVISIBLE) {
base = gd->bd->bi_dram[0].start; base = gd->bd->bi_dram[0].start;
} else {
SYSMEM_E("Failed to alloc invisible sub region 0x%08lx - 0x%08lx "
"of \"%s\" at 0x%08lx - 0x%08lx\n",
(ulong)base, (ulong)gd->bd->bi_dram[0].start,
name, (ulong)base, (ulong)(base + size));
goto out;
}
} }
} else { } else {
SYSMEM_E("Unsupport memblk id %d for alloc sysmem\n", id); SYSMEM_E("Unsupport memblk id %d for alloc sysmem\n", id);
@ -324,14 +424,14 @@ static void *sysmem_alloc_align_base(enum memblk_id id,
base -= ARCH_DMA_MINALIGN; base -= ARCH_DMA_MINALIGN;
} }
if (!IS_ALIGNED(base, 4)) { if (base != SYSMEM_ALLOC_ANYWHERE && !IS_ALIGNED(base, 4)) {
SYSMEM_E("\"%s\" base=0x%08lx is not 4-byte aligned\n", SYSMEM_E("\"%s\" base=0x%08lx is not 4-byte aligned\n",
name, (ulong)base); name, (ulong)base);
goto out; goto out;
} }
/* Must be 4-byte aligned */ /* Must be sizeof(long) byte aligned */
size = ALIGN(size, 4); size = ALIGN(size, sizeof(long));
SYSMEM_D("Enter alloc: \"%s\" 0x%08lx - 0x%08lx\n", SYSMEM_D("Enter alloc: \"%s\" 0x%08lx - 0x%08lx\n",
name, (ulong)base, (ulong)(base + size)); name, (ulong)base, (ulong)(base + size));
@ -350,19 +450,6 @@ static void *sysmem_alloc_align_base(enum memblk_id id,
SYSMEM_E("Failed to double alloc for existence \"%s\"\n", name); SYSMEM_E("Failed to double alloc for existence \"%s\"\n", name);
goto out; goto out;
} else if (sysmem_is_overlap(mem->base, mem->size, base, size)) { } else if (sysmem_is_overlap(mem->base, mem->size, base, size)) {
/*
* If this new alloc region expects overlap and the old
* region is also allowed to be overlap, just do reserve.
*/
if (req_overlap && mem->attr.flags & M_ATTR_OVERLAP) {
if (lmb_reserve(&sysmem->lmb, base, size))
SYSMEM_E("Failed to overlap alloc \"%s\" "
"at 0x%08lx - 0x%08lx\n",
name, (ulong)base,
(ulong)(base + size));
return (void *)base;
}
SYSMEM_E("\"%s\" (0x%08lx - 0x%08lx) alloc is " SYSMEM_E("\"%s\" (0x%08lx - 0x%08lx) alloc is "
"overlap with existence \"%s\" (0x%08lx - " "overlap with existence \"%s\" (0x%08lx - "
"0x%08lx)\n", "0x%08lx)\n",
@ -394,6 +481,8 @@ static void *sysmem_alloc_align_base(enum memblk_id id,
goto out; goto out;
} }
/* Record original base for dump */
mem->orig_base = orig_base;
mem->base = paddr; mem->base = paddr;
mem->size = alloc_size; mem->size = alloc_size;
mem->attr = attr; mem->attr = attr;
@ -433,12 +522,18 @@ static void *sysmem_alloc_align_base(enum memblk_id id,
out: out:
/* /*
* Why: base + sizeof(ulong) ? * Why: base + sizeof(ulong) ?
* It's a not standard way to handle the case: the input base is 0. * It's not a standard way to handle the case: the input base is 0.
* Because 0 equals NULL, but we don't want to return NULL when alloc
* successfully, so just return a !NULL value is okay.
*
* When it happens ?
* Maybe 32-bit platform would alloc region for uncompress kernel
* at 0 address.
*/ */
if (base == 0) if (base == 0)
base = base + sizeof(ulong); base = base + sizeof(ulong);
return (attr.flags & M_ATTR_PEEK) ? (void *)base : NULL; return (attr.flags & M_ATTR_IGNORE_INVISIBLE) ? (void *)base : NULL;
} }
void *sysmem_alloc(enum memblk_id id, phys_size_t size) void *sysmem_alloc(enum memblk_id id, phys_size_t size)
@ -492,7 +587,7 @@ void *sysmem_fdt_reserve_alloc_base(const char *name,
{ {
void *paddr; void *paddr;
paddr = sysmem_alloc_align_base(MEMBLK_ID_FDT_RESV, paddr = sysmem_alloc_align_base(MEMBLK_ID_KMEM_RESERVED,
name, name,
base, base,
size, size,
@ -548,7 +643,7 @@ int sysmem_free(phys_addr_t base)
/* Find existence */ /* Find existence */
list_for_each(node, &sysmem->allocated_head) { list_for_each(node, &sysmem->allocated_head) {
mem = list_entry(node, struct memblock, node); mem = list_entry(node, struct memblock, node);
if (mem->base == base) { if (mem->base == base || mem->orig_base == base) {
found = 1; found = 1;
break; break;
} }
@ -590,7 +685,10 @@ int sysmem_init(void)
lmb_init(&sysmem->lmb); lmb_init(&sysmem->lmb);
INIT_LIST_HEAD(&sysmem->allocated_head); INIT_LIST_HEAD(&sysmem->allocated_head);
INIT_LIST_HEAD(&sysmem->kmem_resv_head);
sysmem->allocated_cnt = 0; sysmem->allocated_cnt = 0;
sysmem->kmem_resv_cnt = 0;
if (gd->flags & GD_FLG_RELOC) { if (gd->flags & GD_FLG_RELOC) {
sysmem->has_initr = true; sysmem->has_initr = true;
} else { } else {