-
Notifications
You must be signed in to change notification settings - Fork 590
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
Allow using mmap for shared linear memory if OS_ENABLE_HW_BOUND_CHECK is disabled #3029
Conversation
@loganek we are considering using os_mmap to allocate memory no matter whether OS_ENABLE_HW_BOUND_CHECK is enabled or not, since currently the code of memory instantiation/de-instantiation is very complex and not easy to maintain, and we may implement wasm64 feature later, which will make them more complex. os_mmap should have been implemented in all the platforms, and using it may greatly simplify the code, and for this PR, we don't need to introduce WAMR_ENABLE_SHARED_MEMORY_MMAP. How do you think? |
how do you plan to implement the realloc case? |
why do you want to use mmap? |
Do you mean realloc memory when memory.grow? Yes, it is a good question, per my understanding, for shared memory, no need to mmap a new memory, just increase the current page count. For non-shared memory, runtime should mmap a new memory, duplicate memory data to it and update the related info, like: wasm-micro-runtime/core/iwasm/common/wasm_memory.c Lines 705 to 716 in bb053e3
|
realloc can be far cheaper than malloc+memcpy+free. |
Thanks a lot, it is good. |
@wenyongh, I think using mmap in all the cases sounds reasonable, and given that |
@loganek ok, it is good to me, let's review the PR and merge it after it is carefully checked. |
Thanks, please review it then and let me know if you have any feedback. As a followup, I can work on migrating to mmap completely later this week or next week. |
@wenyongh looks like the build failed for unrelated reason - would you be able to restart the build? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with minor comment.
void | ||
wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, | ||
uint64 map_size); | ||
|
||
void * | ||
wasm_mmap_linear_memory(uint64_t map_size, uint64 *io_memory_data_size, | ||
char *error_buf, uint32 error_buf_size); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it better to move wasm_munmap_linear_memory
/wasm_mmap_linear_memory
from wasm_runtime_common.h/c to wasm_memory.h/c? We normally implement memory related operations (except shared memory) in the latter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I've reverted this change since I realized that set_error_buf is not defined and we'd have to define it for wasm_memory too. Because I'm planning to refactor the code and use mmap everywhere, I'll keep it where it is now and move things around during the refactoring. I hope that's ok.
5b80152
to
ae41591
Compare
… is disabled For shared memory, the max memory size must be defined in advanced. Re-allocation for growing memory can't be used as it might change the base address, therefore for OS_ENABLE_HW_BOUND_CHECK enabled the memory is mmaped, and if the flag is disabled, the memory is allocated. This change introduces a flag that allows users to use mmap for reserving memory address space even if the OS_ENABLE_HW_BOUND_CHECK is disabled
…ecodealliance#3029) For shared memory, the max memory size must be defined in advanced. Re-allocation for growing memory can't be used as it might change the base address, therefore when OS_ENABLE_HW_BOUND_CHECK is enabled the memory is mmaped, and if the flag is disabled, the memory is allocated. This change introduces a flag that allows users to use mmap for reserving memory address space even if the OS_ENABLE_HW_BOUND_CHECK is disabled.
For shared memory, the max memory size must be defined in advanced. Re-allocation for growing memory can't be used as it might change the base address, therefore for OS_ENABLE_HW_BOUND_CHECK enabled the memory is mmaped, and if the flag is disabled, the memory is allocated. This change introduces a flag that allows users to use mmap for reserving memory address space even if the OS_ENABLE_HW_BOUND_CHECK is disabled.