Repository navigation
zephyr: vpage: add checks against invalid vpage config #11323
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
kv2019i
wants to merge
1
commit into
thesofproject:main
Choose a base branch
from
kv2019i:202610-vpage-init-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,10 @@ static int vpages_alloc_and_map(unsigned int pages, void **ptr) | |
| if (!ptr) | ||
| return -EINVAL; | ||
|
|
||
| /* the virtual region is missing if init found no space for it */ | ||
| if (!vpage_ctx.virtual_region) | ||
| return -ENODEV; | ||
|
|
||
| if (!pages) { | ||
| *ptr = NULL; | ||
| return 0; | ||
|
|
@@ -175,6 +179,9 @@ static int vpages_free_and_unmap(uintptr_t *ptr) | |
| unsigned int pages = 0; | ||
| int ret; | ||
|
|
||
| if (!vpage_ctx.virtual_region) | ||
| return -ENODEV; | ||
|
|
||
| /* check for valid ptr which must be page aligned */ | ||
| CHECKIF(!IS_ALIGNED(ptr, CONFIG_MM_DRV_PAGE_SIZE)) { | ||
| LOG_ERR("error: invalid non aligned page pointer %p", ptr); | ||
|
|
@@ -269,19 +276,52 @@ static int vpage_init(void) | |
| int ret; | ||
|
|
||
| /* create the virtual memory region and add it to the system */ | ||
| size_t remaining_ram = L2_SRAM_BASE + L2_SRAM_SIZE - | ||
| (adsp_mm_get_unused_l2_start_aligned() + | ||
| CONFIG_SOF_ZEPHYR_VIRTUAL_HEAP_REGION_SIZE + | ||
| CONFIG_LIBRARY_REGION_SIZE); | ||
|
|
||
| ret = adsp_add_virtual_memory_region(adsp_mm_get_unused_l2_start_aligned() + | ||
| CONFIG_SOF_ZEPHYR_VIRTUAL_HEAP_REGION_SIZE, | ||
| remaining_ram, VIRTUAL_REGION_VPAGES_ATTR); | ||
| if (ret) | ||
| return ret; | ||
| uintptr_t l2_end = L2_SRAM_BASE + L2_SRAM_SIZE; | ||
| uintptr_t start = adsp_mm_get_unused_l2_start_aligned() + | ||
| CONFIG_SOF_ZEPHYR_VIRTUAL_HEAP_REGION_SIZE; | ||
| uintptr_t end; | ||
| size_t remaining_ram; | ||
|
|
||
| k_mutex_init(&vpage_ctx.lock); | ||
|
|
||
| if (start + CONFIG_LIBRARY_REGION_SIZE + CONFIG_MM_DRV_PAGE_SIZE <= l2_end) { | ||
| /* virtual heap and library regions fit in L2, vpages get the rest */ | ||
| end = l2_end - CONFIG_LIBRARY_REGION_SIZE; | ||
| } else { | ||
| /* | ||
| * TODO: on demand mapping currently only supports | ||
| * cached mappings, so we need to limit vpages to | ||
| * physical L2 size for now until support is added to | ||
| * Zephyr mm_drv_intel_adsp_mtl_tlb.c and friends | ||
| */ | ||
| end = l2_end; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack, fixed in V2. |
||
| } | ||
|
|
||
| /* the image or virtual heap may leave no room, don't let this underflow */ | ||
| if (end < start + CONFIG_MM_DRV_PAGE_SIZE) { | ||
| LOG_WRN("no space left for vpages, start %#lx end %#lx", | ||
| (unsigned long)start, (unsigned long)end); | ||
| return -ENOMEM; | ||
| } | ||
|
|
||
| remaining_ram = end - start; | ||
| if (end > l2_end) { | ||
| uintptr_t image_end = adsp_mm_get_unused_l2_start_aligned(); | ||
| size_t phys_left = image_end < l2_end ? l2_end - image_end : 0; | ||
|
|
||
| remaining_ram = MIN(remaining_ram, phys_left); | ||
| } | ||
|
|
||
| /* revalidate, the physical cap can leave less than a page */ | ||
| if (remaining_ram < CONFIG_MM_DRV_PAGE_SIZE) { | ||
| LOG_ERR("error: no physical L2 left for vpages"); | ||
| return -ENOMEM; | ||
| } | ||
|
|
||
| ret = adsp_add_virtual_memory_region(start, remaining_ram, VIRTUAL_REGION_VPAGES_ATTR); | ||
| if (ret) | ||
| return ret; | ||
|
|
||
| /* now find the virtual region in all memory regions */ | ||
| virtual_memory_regions = sys_mm_drv_query_memory_regions(); | ||
| SYS_MM_DRV_MEMORY_REGION_FOREACH(virtual_memory_regions, region) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Related Zephyr PR to address this limitation zephyrproject-rtos/zephyr#122801