OP-TEE kernel private shared memory optimizations

Optimize OP-TEE driver private shared memory allocated as dynamic shared
 memory. Both to handle larger than one page allocations and for more
 efficient memory usage.
 -----BEGIN PGP SIGNATURE-----
 
 iQJOBAABCgA4FiEEFV+gSSXZJY9ZyuB5LinzTIcAHJcFAmV29qAaHGplbnMud2lr
 bGFuZGVyQGxpbmFyby5vcmcACgkQLinzTIcAHJfvQA/+IV16Zsa5C2VIJXXrfQH/
 p+sc97bsK+waY/Qmn0s6vR6LyCQPSKZPFsZafPv7rxNsYxPrcAzHIKUaEttg2GrL
 86zdZuRjQOgGNccLhBg6C6iaFR6OXWGUNgrdu1/i8Vt61bMpzz8IkrfXZJM6eGtu
 T5jP9QS3T9WmQJI4l3RcjzGrkdFrC6WHcovq2KkFiwCy+7WQhsBDy2aQupwolm3O
 +20UjBKuBmi71qaKMYnwjHz5ECBWG6xRKgf/Xy68r2rFwjZm5yKIuFCLSOQgkuAx
 yYjF1rblCTiZuQ4t4XTIFYNRCsL/ADKCELpnRH5+HcgsipIprDrM+Q9RWJzIzLz4
 p4ebgRHfR7LYe6cD4xDebaI/hzD3ishyCkhpx0f644m+qow1j1BoyVfHrZANl1fy
 5QwmfqT4b3UQWYdx8elaOClzIgL+Coah8Q4/hO13Yg4y5+VjFbITokVnT3ArOJMQ
 Y0SydhPmwrP8E8st85OYKu4QcyB9lNyttqXO0fXQuNj6R7QUJXYKNWAkGpHnrVG6
 DRC+K7JDWbwfhA+BWA0FFb/dOD9iZL+sqTO1r9MHlSCTmM51wGa+4ZP0qiZFE3SG
 wf4FLolGThaauA059piWdXfTI71oVmTEeVrm+Jd8Dv8FOj6fA9v0KneaYbzSKGwO
 NuRsUzVbHj+6cDMM/zGsbcM=
 =Dlu/
 -----END PGP SIGNATURE-----
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEiK/NIGsWEZVxh/FrYKtH/8kJUicFAmWFaIAACgkQYKtH/8kJ
 Uif45Q//VAiPtqwPMYXpTsoO/WEbi/FTjDZ2dKTHgYBx1Vog0IF9cR3n+htzczEu
 gs4Xcdw9EEw/kGYRq/vqLJjyfJo6+qput6eucjsEk0V2KX7CDASvUQe32s//zhBn
 fIYjzgWr0dbvAMgg1THNF7EQiAybawPVG3IG1utEKsluw63CIEjnnqddE9xFfuzG
 XxG0lu0MChLh5YB+AT3bgYqKND/P69eqVZKxAOdOkahSAVZ3eXz+GS2p14G+90eW
 hNE+5UKNtrpCIYxSeG+fwKfandsvgC2eivkExtdTPZKdDW20LGQaS3VsxNIvk1+r
 Oxt/N7fSEafzjrkv+Kv4VulDD7zfZzaNe2yaDXkUcVDizW4ZOIlfjCCf2QKj1Kgr
 NCHZmtb9g+WNfeW7K54bkbPUhUDcVTOcil33JZETqAfGxDTvfj8VHUtcDmEEaNIs
 MVQaDgOVFCC3zO1cPTAUI2h4I6u5664b4ncRXGQDYm/nEt7YWGioSCmGc8o+oCVD
 mkEeWsalUWDvlWuHeCcBXUQtqSqB25AiCh4LNyZRpxBrUAM26UaXC3QCiHTCWQBk
 uUOSVFiaaLYfj9E3350MrnYU10NPmczB+Q2COBKPODv5LNFO/u4cnsqCKIZ5cd9W
 h6MAcjmux1hQegAGOQJxPc8EfqDLcIgMszpDG5hEZATL0dYTjJk=
 =HSTy
 -----END PGP SIGNATURE-----

Merge tag 'kern-priv-shm-for-v6.8' of https://git.linaro.org/people/jens.wiklander/linux-tee into soc/drivers

OP-TEE kernel private shared memory optimizations

Optimize OP-TEE driver private shared memory allocated as dynamic shared
memory. Both to handle larger than one page allocations and for more
efficient memory usage.

* tag 'kern-priv-shm-for-v6.8' of https://git.linaro.org/people/jens.wiklander/linux-tee:
  optee: allocate shared memory with alloc_pages_exact()
  optee: add page list to kernel private shared memory

Link: https://lore.kernel.org/r/20231211115815.GA616539@rayden
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
This commit is contained in:
Arnd Bergmann 2023-12-22 10:44:16 +00:00
commit daa554ab04
2 changed files with 46 additions and 47 deletions

View File

@ -26,46 +26,46 @@ 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);
struct page *page;
size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
struct page **pages;
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) {
rc = -ENOMEM;
goto err;
}
for (i = 0; i < nr_pages; i++)
pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
shm->pages = pages;
shm->num_pages = nr_pages;
if (shm_register) {
unsigned int nr_pages = 1 << order, i;
struct page **pages;
pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
if (!pages) {
rc = -ENOMEM;
goto err;
}
for (i = 0; i < nr_pages; i++)
pages[i] = page + i;
rc = shm_register(shm->ctx, shm, pages, nr_pages,
(unsigned long)shm->kaddr);
kfree(pages);
if (rc)
goto err;
}
return 0;
err:
free_pages((unsigned long)shm->kaddr, order);
free_pages_exact(shm->kaddr, shm->size);
shm->kaddr = NULL;
return rc;
}
@ -75,8 +75,10 @@ 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;
}
static void optee_bus_scan(struct work_struct *work)

View File

@ -678,10 +678,11 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
struct optee_msg_arg *arg,
struct optee_call_ctx *call_ctx)
{
phys_addr_t pa;
struct tee_shm *shm;
size_t sz;
size_t n;
struct page **pages;
size_t page_count;
arg->ret_origin = TEEC_ORIGIN_COMMS;
@ -716,32 +717,23 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
return;
}
if (tee_shm_get_pa(shm, 0, &pa)) {
arg->ret = TEEC_ERROR_BAD_PARAMETERS;
goto bad;
}
sz = tee_shm_get_size(shm);
if (tee_shm_is_dynamic(shm)) {
struct page **pages;
/*
* If there are pages it's dynamically allocated shared memory (not
* from the reserved shared memory pool) and needs to be
* registered.
*/
pages = tee_shm_get_pages(shm, &page_count);
if (pages) {
u64 *pages_list;
size_t page_num;
pages = tee_shm_get_pages(shm, &page_num);
if (!pages || !page_num) {
arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
goto bad;
}
pages_list = optee_allocate_pages_list(page_num);
pages_list = optee_allocate_pages_list(page_count);
if (!pages_list) {
arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
goto bad;
}
call_ctx->pages_list = pages_list;
call_ctx->num_entries = page_num;
call_ctx->num_entries = page_count;
arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
OPTEE_MSG_ATTR_NONCONTIG;
@ -752,17 +744,22 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
(tee_shm_get_page_offset(shm) &
(OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
arg->params[0].u.tmem.size = tee_shm_get_size(shm);
arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
optee_fill_pages_list(pages_list, pages, page_num,
optee_fill_pages_list(pages_list, pages, page_count,
tee_shm_get_page_offset(shm));
} else {
phys_addr_t pa;
if (tee_shm_get_pa(shm, 0, &pa)) {
arg->ret = TEEC_ERROR_BAD_PARAMETERS;
goto bad;
}
arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
arg->params[0].u.tmem.buf_ptr = pa;
arg->params[0].u.tmem.size = sz;
arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
}
arg->params[0].u.tmem.size = tee_shm_get_size(shm);
arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
arg->ret = TEEC_SUCCESS;
return;