Skip to content

Commit

Permalink
xen/arm: Reduce struct membank size on static shared memory
Browse files Browse the repository at this point in the history
Currently the memory footprint of the static shared memory feature
is impacting all the struct meminfo instances with memory space
that is not going to be used.

To solve this issue, rework the static shared memory extra
information linked to the memory bank to another structure,
struct shmem_membank_extra, and exploit the struct membank
padding to host a pointer to that structure in a union with the
enum membank_type, with this trick the 'struct membank' has the
same size with or without the static shared memory, given that
the 'type' and 'shmem_extra' are never used at the same time,
hence the 'struct membank' won't grow in size.

Afterwards, create a new structure 'struct shared_meminfo' which
has the same interface of 'struct meminfo', but requires less
banks, defined by the number in NR_SHMEM_BANKS, which is 32 at the
moment and should be enough for the current use cases, the value
might be increased in te future if needed.
Finally, this structure hosts also the extra information for the
static shared memory banks.
The fields 'bank' and 'extra' of this structure are meant to be
linked by the index (e.g. extra[idx] will have the information for
the bank[idx], for i=0..NR_SHMEM_BANKS), the convinient pointer
'shmem_extra' of 'struct membank' is then linked to the related
'extra' bank to ease the fruition when a function has access only
to the 'struct membanks common' of 'struct shared_meminfo'.

The last part of this work is to move the allocation of the
static shared memory banks from the 'reserved_mem' to a new
'shmem' member of the 'struct bootinfo'.
Change also the 'shm_mem' member type to be 'struct shared_meminfo'
in order to match the above changes and allow a memory space
reduction also in 'struct kernel_info'.

Now that the structure

Signed-off-by: Luca Fancellu <luca.fancellu@arm.com>
Reviewed-by: Michal Orzel <michal.orzel@amd.com>
  • Loading branch information
luca-fancellu authored and Julien Grall committed Apr 24, 2024
1 parent e5816ad commit 2b557af
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 24 deletions.
24 changes: 24 additions & 0 deletions xen/arch/arm/arm32/mmu/mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <xen/pfn.h>
#include <asm/fixmap.h>
#include <asm/static-memory.h>
#include <asm/static-shmem.h>

static unsigned long opt_xenheap_megabytes __initdata;
integer_param("xenheap_megabytes", opt_xenheap_megabytes);
Expand Down Expand Up @@ -43,6 +44,9 @@ static paddr_t __init consider_modules(paddr_t s, paddr_t e,
int first_mod)
{
const struct membanks *reserved_mem = bootinfo_get_reserved_mem();
#ifdef CONFIG_STATIC_SHM
const struct membanks *shmem = bootinfo_get_shmem();
#endif
const struct bootmodules *mi = &bootinfo.modules;
int i;
int nr;
Expand Down Expand Up @@ -119,6 +123,25 @@ static paddr_t __init consider_modules(paddr_t s, paddr_t e,
return consider_modules(s, r_s, size, align, i + 1);
}
}

#ifdef CONFIG_STATIC_SHM
nr += reserved_mem->nr_banks;
for ( ; i - nr < shmem->nr_banks; i++ )
{
paddr_t r_s = shmem->bank[i - nr].start;
paddr_t r_e = r_s + shmem->bank[i - nr].size;

if ( s < r_e && r_s < e )
{
r_e = consider_modules(r_e, e, size, align, i + 1);
if ( r_e )
return r_e;

return consider_modules(s, r_s, size, align, i + 1);
}
}
#endif

return e;
}

Expand Down Expand Up @@ -295,6 +318,7 @@ void __init setup_mm(void)
mfn_to_maddr(directmap_mfn_end));

init_staticmem_pages();
init_sharedmem_pages();
}

/*
Expand Down
2 changes: 2 additions & 0 deletions xen/arch/arm/arm64/mmu/mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <asm/setup.h>
#include <asm/static-memory.h>
#include <asm/static-shmem.h>

/* Override macros from asm/page.h to make them work with mfn_t */
#undef virt_to_mfn
Expand Down Expand Up @@ -236,6 +237,7 @@ void __init setup_mm(void)
max_page = PFN_DOWN(ram_end);

init_staticmem_pages();
init_sharedmem_pages();
}

/*
Expand Down
1 change: 1 addition & 0 deletions xen/arch/arm/bootfdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ static void __init early_print_info(void)
mem_resv->bank[j].start,
mem_resv->bank[j].start + mem_resv->bank[j].size - 1);
}
early_print_info_shmem();
printk("\n");
for ( i = 0 ; i < cmds->nr_mods; i++ )
printk("CMDLINE[%"PRIpaddr"]:%s %s\n", cmds->cmdline[i].start,
Expand Down
5 changes: 5 additions & 0 deletions xen/arch/arm/domain_build.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ static int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn,
* regions we exclude every region assigned to Dom0 from the Host RAM:
* - domain RAM
* - reserved-memory
* - static shared memory
* - grant table space
*/
static int __init find_unallocated_memory(const struct kernel_info *kinfo,
Expand All @@ -872,6 +873,9 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo,
const struct membanks *mem_banks[] = {
kernel_info_get_mem(kinfo),
bootinfo_get_reserved_mem(),
#ifdef CONFIG_STATIC_SHM
bootinfo_get_shmem(),
#endif
};
struct rangeset *unalloc_mem;
paddr_t start, end;
Expand Down Expand Up @@ -903,6 +907,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo,
* Exclude the following regions:
* 1) Remove RAM assigned to Dom0
* 2) Remove reserved memory
* 3) Remove static shared memory (when the feature is enabled)
*/
for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
for ( j = 0; j < mem_banks[i]->nr_banks; j++ )
Expand Down
4 changes: 2 additions & 2 deletions xen/arch/arm/include/asm/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct kernel_info {
paddr_t unassigned_mem; /* RAM not (yet) assigned to a bank */
struct meminfo mem;
#ifdef CONFIG_STATIC_SHM
struct meminfo shm_mem;
struct shared_meminfo shm_mem;
#endif

/* kernel entry point */
Expand Down Expand Up @@ -83,7 +83,7 @@ struct kernel_info {
#define kernel_info_get_mem(kinfo) (&(kinfo)->mem.common)

#ifdef CONFIG_STATIC_SHM
#define KERNEL_INFO_SHM_MEM_INIT .shm_mem.common.max_banks = NR_MEM_BANKS,
#define KERNEL_INFO_SHM_MEM_INIT .shm_mem.common.max_banks = NR_SHMEM_BANKS,
#else
#define KERNEL_INFO_SHM_MEM_INIT
#endif
Expand Down
41 changes: 38 additions & 3 deletions xen/arch/arm/include/asm/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define MAX_FDT_SIZE SZ_2M

#define NR_MEM_BANKS 256
#define NR_SHMEM_BANKS 32

#define MAX_MODULES 32 /* Current maximum useful modules */

Expand Down Expand Up @@ -46,14 +47,20 @@ enum membank_type {
/* Indicates the maximum number of characters(\0 included) for shm_id */
#define MAX_SHM_ID_LENGTH 16

struct shmem_membank_extra {
char shm_id[MAX_SHM_ID_LENGTH];
unsigned int nr_shm_borrowers;
};

struct membank {
paddr_t start;
paddr_t size;
enum membank_type type;
union {
enum membank_type type;
#ifdef CONFIG_STATIC_SHM
char shm_id[MAX_SHM_ID_LENGTH];
unsigned int nr_shm_borrowers;
struct shmem_membank_extra *shmem_extra;
#endif
};
};

struct membanks {
Expand All @@ -67,6 +74,12 @@ struct meminfo {
struct membank bank[NR_MEM_BANKS];
};

struct shared_meminfo {
struct membanks common;
struct membank bank[NR_SHMEM_BANKS];
struct shmem_membank_extra extra[NR_SHMEM_BANKS];
};

/*
* The domU flag is set for kernels and ramdisks of "xen,domain" nodes.
* The purpose of the domU flag is to avoid getting confused in
Expand Down Expand Up @@ -109,6 +122,9 @@ struct bootinfo {
struct bootcmdlines cmdlines;
#ifdef CONFIG_ACPI
struct meminfo acpi;
#endif
#ifdef CONFIG_STATIC_SHM
struct shared_meminfo shmem;
#endif
bool static_heap;
};
Expand All @@ -119,11 +135,18 @@ struct bootinfo {
#define BOOTINFO_ACPI_INIT
#endif

#ifdef CONFIG_STATIC_SHM
#define BOOTINFO_SHMEM_INIT .shmem.common.max_banks = NR_SHMEM_BANKS,
#else
#define BOOTINFO_SHMEM_INIT
#endif

#define BOOTINFO_INIT \
{ \
.mem.common.max_banks = NR_MEM_BANKS, \
.reserved_mem.common.max_banks = NR_MEM_BANKS, \
BOOTINFO_ACPI_INIT \
BOOTINFO_SHMEM_INIT \
}

struct map_range_data
Expand Down Expand Up @@ -158,6 +181,18 @@ static inline struct membanks *bootinfo_get_acpi(void)
}
#endif

#ifdef CONFIG_STATIC_SHM
static inline struct membanks *bootinfo_get_shmem(void)
{
return &bootinfo.shmem.common;
}

static inline struct shmem_membank_extra *bootinfo_get_shmem_extra(void)
{
return bootinfo.shmem.extra;
}
#endif

void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len);

size_t estimate_efi_size(unsigned int mem_nr_banks);
Expand Down
8 changes: 8 additions & 0 deletions xen/arch/arm/include/asm/static-shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ static inline int process_shm_chosen(struct domain *d,
int process_shm_node(const void *fdt, int node, uint32_t address_cells,
uint32_t size_cells);

void early_print_info_shmem(void);

void init_sharedmem_pages(void);

#else /* !CONFIG_STATIC_SHM */

static inline int make_resv_memory_node(const struct kernel_info *kinfo,
Expand Down Expand Up @@ -51,6 +55,10 @@ static inline int process_shm_node(const void *fdt, int node,
return -EINVAL;
}

static inline void early_print_info_shmem(void) {};

static inline void init_sharedmem_pages(void) {};

#endif /* CONFIG_STATIC_SHM */

#endif /* __ASM_STATIC_SHMEM_H_ */
Expand Down
25 changes: 24 additions & 1 deletion xen/arch/arm/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ static void __init dt_unreserved_regions(paddr_t s, paddr_t e,
unsigned int first)
{
const struct membanks *reserved_mem = bootinfo_get_reserved_mem();
#ifdef CONFIG_STATIC_SHM
const struct membanks *shmem = bootinfo_get_shmem();
#endif
unsigned int i, nr;
int rc;

Expand Down Expand Up @@ -258,6 +261,22 @@ static void __init dt_unreserved_regions(paddr_t s, paddr_t e,
}
}

#ifdef CONFIG_STATIC_SHM
nr += reserved_mem->nr_banks;
for ( ; i - nr < shmem->nr_banks; i++ )
{
paddr_t r_s = shmem->bank[i - nr].start;
paddr_t r_e = r_s + shmem->bank[i - nr].size;

if ( s < r_e && r_s < e )
{
dt_unreserved_regions(r_e, e, cb, i + 1);
dt_unreserved_regions(s, r_s, cb, i + 1);
return;
}
}
#endif

cb(s, e);
}

Expand Down Expand Up @@ -344,13 +363,17 @@ bool __init check_reserved_regions_overlap(paddr_t region_start,
bootinfo_get_reserved_mem(),
#ifdef CONFIG_ACPI
bootinfo_get_acpi(),
#endif
#ifdef CONFIG_STATIC_SHM
bootinfo_get_shmem(),
#endif
};
unsigned int i;

/*
* Check if input region is overlapping with reserved memory banks or
* ACPI EfiACPIReclaimMemory (when ACPI feature is enabled)
* ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static
* shared memory banks (when static shared memory feature is enabled)
*/
for ( i = 0; i < ARRAY_SIZE(mem_banks); i++ )
if ( meminfo_overlap_check(mem_banks[i], region_start, region_size) )
Expand Down

0 comments on commit 2b557af

Please sign in to comment.