[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20231108102755.93079-3-jens.wiklander@linaro.org>
Date: Wed, 8 Nov 2023 11:27:55 +0100
From: Jens Wiklander <jens.wiklander@...aro.org>
To: linux-kernel@...r.kernel.org, op-tee@...ts.trustedfirmware.org
Cc: Sumit Garg <sumit.garg@...aro.org>,
Jerome Forissier <jerome.forissier@...aro.org>,
Shyam Saini <shyamsaini@...ux.microsoft.com>,
Jens Wiklander <jens.wiklander@...aro.org>
Subject: [PATCH v2 2/2] optee: allocate shared memory with alloc_pages_exact()
Allocate memory to share with the secure using alloc_pages_exact()
instead of alloc_pages() for more efficient memory usage.
Signed-off-by: Jens Wiklander <jens.wiklander@...aro.org>
---
drivers/tee/optee/core.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c
index a425eca0173d..6303085a1b49 100644
--- a/drivers/tee/optee/core.c
+++ b/drivers/tee/optee/core.c
@@ -26,21 +26,19 @@ 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;
- struct page *page;
+ size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
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->size = PAGE_SIZE << order;
+ shm->size = nr_pages * PAGE_SIZE;
/*
* If memory is registered immediately use a temporary page list
@@ -66,7 +64,8 @@ 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);
if (shm_register) {
rc = shm_register(shm->ctx, shm, pages, nr_pages,
@@ -79,12 +78,12 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
shm->num_pages = nr_pages;
}
} else {
- shm->paddr = page_to_phys(page);
+ shm->paddr = virt_to_phys(shm->kaddr);
}
return 0;
err:
- free_pages((unsigned long)shm->kaddr, order);
+ free_pages_exact(shm->kaddr, shm->size);
shm->kaddr = NULL;
return rc;
}
@@ -95,7 +94,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;
--
2.34.1
Powered by blists - more mailing lists