Skip to content

Request for comments: Expand sbrk to allocate memory from two regions #9441

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

Closed
wants to merge 1 commit into from
Closed

Request for comments: Expand sbrk to allocate memory from two regions #9441

wants to merge 1 commit into from

Conversation

marcuschangarm
Copy link
Contributor

Description

Some MCUs, like the STM32L475, have multiple discrete RAM regions. Accessing memory across these boundaries will cause a hard fault. The current sbrk implementation for GCC only allocates heap from one of these regions, leaving unused memory in other regions unavailable.

The change here allows sbrk to allocate from the second region once the first is full and assumes that malloc is able to handle the discontinuity in assigned heap.

Question: The sbrk documentation is not clear on whether this discontinuity is allowed behavior or dependent on the malloc implementation.

From https://linux.die.net/man/2/sbrk:

On success, sbrk() returns the previous program break. (If the break was increased, then this value is a pointer to the start of the newly allocated memory). On error, (void *) -1 is returned, and errno is set to ENOMEM.

Pull request type

[x] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

@ciarmcom
Copy link
Member

@marcuschangarm, thank you for your changes.
@ARMmbed/mbed-os-maintainers please review.

@ciarmcom ciarmcom requested a review from a team January 20, 2019 20:00
@0xc0170 0xc0170 requested a review from a team January 21, 2019 08:21
Copy link
Collaborator

@jeromecoutant jeromecoutant left a comment

Choose a reason for hiding this comment

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

Hi

  • DISCO_L475VG_IOT01A: tests-mbedmicro-rtos-mbed-heap_and_stack becomes FAIL

  • other targets supporting TWO_RAM_REGIONS: build is failing

@marcuschangarm
Copy link
Contributor Author

Hi @jeromecoutant!
This isn't ready as a PR yet. What I'm looking for is an opinion on the approach: is this a valid interpretation of how sbrk is supposed to work? Thanks!

@kjbracey
Copy link
Contributor

If a library asks you to provide sbrk, the natural assumption is that you're allowed to provide discontiguous responses. That's the reason for the form of the API.

I've looked at the newlib-nano malloc code, I can see no limitations here. Discontiguous heap is no big deal, particularly for a simple heap allocator - malloc just needs to add the new sbrk region to its free block list - it doesn't matter whether if the total free list could ever be combined into one single contiguous area.

My only real query is what is that existing "l4_retarget.c" code doing there if it's not already providing two ram regions? Looking in the history, it seems the target-specific sbrk support hook was added to Mbed OS, and activated for the L4, but no-one ever actually made it do anything until now?

I'm concerned there may be some background here we may be missing - some reason why it's not already active.

@marcuschangarm
Copy link
Contributor Author

marcuschangarm commented Jan 22, 2019

@kjbracey-arm Thank you for the input!

Perhaps we should have a standard way to handle multiple RAM regions in Mbed OS since this is not specific to the STM32L4 and I'm not sure how to reconcile multiple RAM regions in tests-mbedmicro-rtos-mbed-heap_and_stack?

@kjbracey
Copy link
Contributor

Well, that "TWO_RAM_REGIONS" define is a general hook - if defined, the target gets to provide their own totally-custom sbrk (for however many regions, despite the name). So it's similar to "MBED_MPU_CUSTOM". Would better be called "MBED_SBRK_CUSTOM" or "MBED_HEAP_CUSTOM".

But like for the MPU, providing a complete replacement is a fiddle, so yes, it might be nice to have built-in handling for 2 regions based purely on fixed area names.

If you did have such built-in handling, then those tests could handle it. But with the custom generic handling in use I think those tests just have to be deactivated.

@marcuschangarm
Copy link
Contributor Author

As it sounds like the approach is valid but would require tighter integration with the rest of Mbed OS and other targets, I'm moving this over to issues. Thank you for all the input.

@juhoeskeli
Copy link
Contributor

@marcuschangarm @kjbracey-arm we really need mechanism like this to utilise whole target RAM. Please keep me posted as well if something that I can use is inbound. I am actually hoping to use this particular target, but am being blocked by lack of memory due to two ram regions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants