Skip to content

Commit

Permalink
optee: allocate shared memory with alloc_pages_exact()
Browse files Browse the repository at this point in the history
Allocate memory to share with the secure world using alloc_pages_exact()
instead of alloc_pages() for more efficient memory usage.

Reviewed-by: Sumit Garg <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
  • Loading branch information
jenswi-linaro committed Dec 4, 2023
1 parent 69724b3 commit 225a36b
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions drivers/tee/optee/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,22 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
size_t num_pages,
unsigned long start))
{
unsigned int order = get_order(size);
unsigned int nr_pages = 1 << order;
size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
struct page **pages;
struct page *page;
unsigned int i;
int rc = 0;

/*
* Ignore alignment since this is already going to be page aligned
* and there's no need for any larger alignment.
*/
page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
if (!page)
shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
GFP_KERNEL | __GFP_ZERO);
if (!shm->kaddr)
return -ENOMEM;

shm->kaddr = page_address(page);
shm->paddr = page_to_phys(page);
shm->size = PAGE_SIZE << order;
shm->paddr = virt_to_phys(shm->kaddr);
shm->size = nr_pages * PAGE_SIZE;

pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
if (!pages) {
Expand All @@ -52,7 +50,7 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
}

for (i = 0; i < nr_pages; i++)
pages[i] = page + i;
pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);

shm->pages = pages;
shm->num_pages = nr_pages;
Expand All @@ -66,7 +64,7 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,

return 0;
err:
free_pages((unsigned long)shm->kaddr, order);
free_pages_exact(shm->kaddr, shm->size);
shm->kaddr = NULL;
return rc;
}
Expand All @@ -77,7 +75,7 @@ void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
{
if (shm_unregister)
shm_unregister(shm->ctx, shm);
free_pages((unsigned long)shm->kaddr, get_order(shm->size));
free_pages_exact(shm->kaddr, shm->size);
shm->kaddr = NULL;
kfree(shm->pages);
shm->pages = NULL;
Expand Down

0 comments on commit 225a36b

Please sign in to comment.