Skip to content

llext: add dynamic heap allocation support #90763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexivanov-google
Copy link

Some applications require loading extensions into the memory which does not exist during the boot time and cannot be allocated statically. Make the application responsible for LLEXT heap allocation. Do not allocate LLEXT heap statically.

Copy link

Hello @alexivanov-google, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@github-actions github-actions bot added the area: llext Linkable Loadable Extensions label May 28, 2025
@github-actions github-actions bot requested review from lyakh, pillo79 and teburd May 28, 2025 18:44
@teburd
Copy link
Collaborator

teburd commented May 28, 2025

Please squash commits

Some applications require loading extensions into the memory which does
not exist during the boot time and cannot be allocated statically. Make
the application responsible for LLEXT heap allocation. Do not allocate
LLEXT heap statically.

Signed-off-by: Alex Ivanov <alexivanov@google.com>
@alexivanov-google
Copy link
Author

squashed

Copy link

@alexivanov-google
Copy link
Author

Do I need to trigger presubmit rerun somehow? Could not find the right "button" :)

Copy link
Collaborator

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I have a few minor nitpicks on the current code, and two bigger questions for you:

  • can you give a brief usage example? Are there any alternatives to basically pasting the same line in the app?
  • what happens if you call LLEXT APIs while the heap is not allocated? I was expecting additional checks at least in llext_mem.c. Is that implicit with heap APIs?

@@ -60,6 +60,14 @@ config LLEXT_BUILD_PIC
most internal linking is performed by the linker at build time. Select "y"
to make use of that advantage.

config LLEXT_HEAP_DYNAMIC
bool "Do not allocate static LLEXT heap"
default n
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default n is implicit, can be removed

Comment on lines +67 to +69
Some applications require loading extensions into the memory which does not
exist during the boot time and cannot be allocated statically. Make the application
responsible for LLEXT heap allocation. Do not allocate LLEXT heap statically.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mention how to do this as well. I suppose you need to define llext_heap in the app?

@@ -60,6 +60,14 @@ config LLEXT_BUILD_PIC
most internal linking is performed by the linker at build time. Select "y"
to make use of that advantage.

config LLEXT_HEAP_DYNAMIC
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this before LLEXT_HEAP_SIZE. Make that entry conditional on !LLEXT_HEAP_DYNAMIC, since specifying the size becomes useless with a dynamic heap.

@alexivanov-google
Copy link
Author

Hi Luca,

thanks for looking at the patch!

My system does not have enough RAM to execute the code that is only needed for the boot-time hardware initialization. LLEXT to the rescue: I build the code that runs the initialization flow as an LLEXT module. During my boot time I can configure a temporary scratchpad memory bank. This is done dynamically, the scratchpad is not available during the main application body loading phase. So now, comes the problem with the heap, as I don't want to allocate the heap in the main RAM, it is only needed during the LLEXT execution phase, after which the scratchpad (and the LLEXT stack) disappears. So I am declaring the LLEXT heap in the app and initialize the heap in the scratchpad memory as follows:

/* setup the scratchpad memory bank */
struct k_heap llext_heap;
k_heap_init(&llext_heap, scratchpad_addr, llext_heap_size);
/* load my extension, execute out of the scratchpad */
/* deallocate the scratchpad memory bank */

Currently if the heap is not initialized by the application, the application crashes, the heap API is not checking for null pointers. If we are to check for a null pointer in the LLEXT API, I have some questions:

  1. Should I check in llext_load() or is there a better place to check?
  2. How can I check for application failure to initialize the llext_heap? I do not see an API such as k_heap_is_initialized(& llext_heap). I can write one, but should it be based on checking which field of struct k_heap?
  3. Should I declare a dynamic heap inside the LLEXT subsystem code instead? Then I can pass the heap address/size in struct llext_load_param to llext_load() and allocate the heap there?
  4. Make another LLEXT API call to allocate the heap, or do something else to allocate/place the LLEXT heap onto specific address?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: llext Linkable Loadable Extensions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants