Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions zephyr/lib/vpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,39 @@ __cold static int vpage_init(void)
end = l2_end - CONFIG_LIBRARY_REGION_SIZE;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a blocker but I wonder if the vpages should go beyond l2_end in this case. IOW why limit vpages to physical L2 size in this case, if we already know Zephyr supports going beyond and we use it below for systems with smaller L2. IOW, shouldn't we size vpages based on a) physical L2 size and b) image size. vpages cannot be larger than a-b, but I don't see a need to limit otherwise.

} 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
* Small L2 (e.g. NVL-S): the virtual heap and library regions are only
* virtual address space backed on demand, so they need not fit in L2
* together with the vpages. Use the virtual space up to the library
* region (or the end of virtual memory) and cap it below by the
* physical L2 left after the image, as vpages can never use more.
* Pages beyond physical L2 are accessed through the coherent alias
* too, which needs the Zephyr TLB driver to map the uncached alias.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The last sentence is not super useful. More useful would be to replace with a link to the Zephyr PR -- the main point is Zephyr had a bug in using addresses beyond physical L2 end.

*/
LOG_WRN("no space left for vpages, start %#lx l2_end %#lx",
(unsigned long)start, (unsigned long)l2_end);
end = L2_VIRTUAL_SRAM_BASE + L2_VIRTUAL_SRAM_SIZE;
if (IS_ENABLED(CONFIG_LIBRARY_MANAGER) && CONFIG_LIBRARY_BASE_ADDRESS < end)
end = CONFIG_LIBRARY_BASE_ADDRESS;
}
Comment thread
kv2019i marked this conversation as resolved.
Resolved

/* 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;
__ASSERT_NO_MSG(remaining_ram >= CONFIG_MM_DRV_PAGE_SIZE);
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_WRN("no physical L2 left for vpages");
return -ENOMEM;
}

ret = adsp_add_virtual_memory_region(start, remaining_ram, VIRTUAL_REGION_VPAGES_ATTR);
if (ret)
Expand Down
33 changes: 33 additions & 0 deletions zephyr/test/vpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,36 @@ ZTEST(sof_boot, vpage)

vpage_free(p1);
}

/* Freeing a non-last allocation must not orphan the last tracked element */
ZTEST(sof_boot, vpage_free_out_of_order)
{
void *a = vpage_alloc(1);
void *b = vpage_alloc(1);
void *c, *d;

zassert_not_null(a);
zassert_not_null(b);

/* the check below needs a and b to be neighbours, first fit normally does this */
if ((uint8_t *)b != (uint8_t *)a + CONFIG_MM_DRV_PAGE_SIZE) {
vpage_free(a);
vpage_free(b);
ztest_test_skip();
return;
}

vpage_free(a);

/* takes a's page; must not overwrite b's tracking element */
c = vpage_alloc(1);
zassert_equal(c, a);

vpage_free(b);
vpage_free(c);

/* if b's element was lost its page is leaked and the 2 page range can't be reused */
d = vpage_alloc(2);
zassert_equal(d, a, "pages leaked after out of order free");
vpage_free(d);
}
Loading