modlib: Always use separate allocation for text and data

Pros:

* Reduce code differences
* Smaller allocations for !CONFIG_ARCH_USE_MODULE_TEXT

Cons:

* Likely to use more memory for !CONFIG_ARCH_USE_MODULE_TEXT in total

Tested with:

* sim:module on macOS
* esp32-devkit:nsh + CONFIG_MODULE on qemu
* lm3s6965-ek:qemu-protected + CONFIG_EXAMPLES_SOTEST on qemu
This commit is contained in:
YAMAMOTO Takashi 2021-04-15 11:33:48 +09:00 committed by Xiang Xiao
parent 1a9e7efde5
commit 418e11b8b3
8 changed files with 30 additions and 65 deletions

View File

@ -159,12 +159,8 @@ struct module_s
mod_initializer_t initializer; /* Module initializer function */
#endif
struct mod_info_s modinfo; /* Module information */
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
FAR void *textalloc; /* Allocated kernel text memory */
FAR void *dataalloc; /* Allocated kernel memory */
#else
FAR void *alloc; /* Allocated kernel memory */
#endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
size_t textsize; /* Size of the kernel .text memory allocation */
size_t datasize; /* Size of the kernel .bss/.data memory allocation */

View File

@ -112,17 +112,30 @@ static inline int dlremove(FAR void *handle)
/* Release resources held by the module */
if (modp->alloc != NULL)
if (modp->textalloc != NULL)
{
/* Free the module memory */
lib_free((FAR void *)modp->alloc);
lib_free((FAR void *)modp->textalloc);
/* Nullify so that the memory cannot be freed again */
modp->alloc = NULL;
modp->textalloc = NULL;
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
modp->textsize = 0;
#endif
}
if (modp->dataalloc != NULL)
{
/* Free the module memory */
lib_free((FAR void *)modp->dataalloc);
/* Nullify so that the memory cannot be freed again */
modp->dataalloc = NULL;
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
modp->datasize = 0;
#endif
}

View File

@ -208,7 +208,8 @@ static inline FAR void *dlinsert(FAR const char *filename)
/* Save the load information */
modp->alloc = (FAR void *)loadinfo.textalloc;
modp->textalloc = (FAR void *)loadinfo.textalloc;
modp->dataalloc = (FAR void *)loadinfo.datastart;
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
modp->textsize = loadinfo.textsize;
modp->datasize = loadinfo.datasize;

View File

@ -241,10 +241,6 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo)
int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
{
#if !defined(CONFIG_ARCH_USE_MODULE_TEXT)
size_t align;
size_t text_size;
#endif
int ret;
binfo("loadinfo: %p\n", loadinfo);
@ -267,12 +263,16 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
/* Allocate memory to hold the ELF image */
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
if (loadinfo->textsize > 0)
{
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
loadinfo->textalloc = (uintptr_t)
up_module_text_memalign(loadinfo->textalign,
loadinfo->textsize);
#else
loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign,
loadinfo->textsize);
#endif
if (!loadinfo->textalloc)
{
berr("ERROR: Failed to allocate memory for the module text\n");
@ -292,31 +292,6 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
goto errout_with_buffers;
}
}
#else
align = loadinfo->textalign;
if (align < loadinfo->dataalign)
{
align = loadinfo->dataalign;
}
text_size = loadinfo->textsize;
if (loadinfo->datasize > 0)
{
text_size = _ALIGN_UP(text_size, loadinfo->dataalign);
}
loadinfo->textalloc = (uintptr_t)lib_memalign(align,
text_size +
loadinfo->datasize);
if (!loadinfo->textalloc)
{
berr("ERROR: Failed to allocate memory for the module\n");
ret = -ENOMEM;
goto errout_with_buffers;
}
loadinfo->datastart = loadinfo->textalloc + text_size;
#endif
/* Load ELF section data into memory */

View File

@ -58,22 +58,19 @@ int modlib_unload(struct mod_loadinfo_s *loadinfo)
/* Release memory holding the relocated ELF image */
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
if (loadinfo->textalloc != 0)
{
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
up_module_text_free((FAR void *)loadinfo->textalloc);
#else
lib_free((FAR void *)loadinfo->textalloc);
#endif
}
if (loadinfo->datastart != 0)
{
lib_free((FAR void *)loadinfo->datastart);
}
#else
if (loadinfo->textalloc != 0)
{
lib_free((FAR void *)loadinfo->textalloc);
}
#endif
/* Clear out all indications of the allocated address environment */

View File

@ -224,12 +224,8 @@ FAR void *insmod(FAR const char *filename, FAR const char *modname)
/* Save the load information */
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
modp->textalloc = (FAR void *)loadinfo.textalloc;
modp->dataalloc = (FAR void *)loadinfo.datastart;
#else
modp->alloc = (FAR void *)loadinfo.textalloc;
#endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
modp->textsize = loadinfo.textsize;
modp->datasize = loadinfo.datasize;

View File

@ -138,17 +138,9 @@ static int modprocfs_callback(FAR struct module_s *modp, FAR void *arg)
modp->modname, modp->initializer,
modp->modinfo.uninitializer, modp->modinfo.arg,
modp->modinfo.nexports,
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
modp->textalloc,
#else
modp->alloc,
#endif
(unsigned long)modp->textsize,
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
(FAR uint8_t *)modp->dataalloc,
#else
(FAR uint8_t *)modp->alloc + modp->textsize,
#endif
(unsigned long)modp->datasize);
copysize = procfs_memcpy(priv->line, linesize, priv->buffer,
priv->remaining, &priv->offset);

View File

@ -113,11 +113,7 @@ int rmmod(FAR void *handle)
/* Release resources held by the module */
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
if (modp->textalloc != NULL || modp->dataalloc != NULL)
#else
if (modp->alloc != NULL)
#endif
{
/* Free the module memory
* and nullify so that the memory cannot be freed again
@ -125,13 +121,12 @@ int rmmod(FAR void *handle)
#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
up_module_text_free((FAR void *)modp->textalloc);
#else
kmm_free((FAR void *)modp->textalloc);
#endif
kmm_free((FAR void *)modp->dataalloc);
modp->textalloc = NULL;
modp->dataalloc = NULL;
#else
kmm_free((FAR void *)modp->alloc);
modp->alloc = NULL;
#endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
modp->textsize = 0;
modp->datasize = 0;