tee: shared memory updates
Subsystems (8)
v4 cover tee: shared memory updates
Hi all,
This patchset is a general cleanup of shared memory handling in the TEE subsystem.
Until now has the in-kernel tee clients used tee_shm_alloc() and tee_shm_register() to share memory with secure world. These two function exposes via a flags parameter a bit more of the internals of the TEE subsystem than one would like. So in order to make things easier are those two functions replaced by few functions which should provide better abstraction.
Two in-kernel tee clients are updated to use these new functions.
The shared memory pool handling is simplified, an internal matter for the two TEE drivers OP-TEE and AMDTEE.
In the v3 review it was suggested [1] to break out "optee: add driver private tee_context" and "optee: use driver internal tee_contex for some rpc" into a separate patch to fix to allow those a faster path upstream as they fix reported problems. So this patchset is now rebased on top of those patches separated.
This patchset is based on [2] and is also available at [3].
Thanks,
Jens
[1] https://lore.kernel.org/lkml/20220125162938.838382-1-jens.wiklander(a)linaro.org/T/#m9e38c5788b49ed3929276df69fe856b6cbe14dfb
[2] https://git.linaro.org/people/jens.wiklander/linux-tee.git/log/?h=fixes
[3] https://git.linaro.org/people/jens.wiklander/linux-tee.git/log/?h=tee_shm_v4
v3->v4:
* Broke out "optee: add driver private tee_context" and "optee: use driver internal tee_contex for some rpc" into a separate patch as that fixes earlier reported issues and deserves a to go into v5.17 and stable trees.
* Rebased on the recent fixes for the OP-TEE driver on top of v5.17-rc2
* All patches are now reviewed by Sumit Garg + some small fixes from the last review
v2->v3:
* Make tee_shm_alloc_user_buf() and tee_shm_register_user_buf() internal and don't export them to the drivers.
* Rename tee_shm_alloc_priv_kernel_buf() to tee_shm_alloc_priv_buf()
* Adressing comments on variable names and choice of types in "tee: replace tee_shm_register()"
* Adding detailed explaination on alignment in "tee: simplify shm pool handling"
* Added Sumits R-B on a few of the patches
v1->v2:
* The commits three "tee: add tee_shm_alloc_kernel_buf()", "tpm_ftpm_tee: use tee_shm_alloc_kernel_buf()" and
"firmware: tee_bnxt: use tee_shm_alloc_kernel_buf()" has been merged some time ago as part of another patchset.
* Another in-kernel tee client is updated with the commit
"KEYS: trusted: tee: use tee_shm_register_kernel_buf()"
* tee_shm_alloc_anon_kernel_buf() is replaced with an easier to use function tee_shm_alloc_priv_kernel_buf() and tee_shm_free_anon_kernel_buf() has been dropped.
* A driver internal struct tee_context is used to when doing driver internal calls to secure world.
* Adds patches to replace tee_shm_register() in a similar way as how tee_shm_alloc() is replaced.
* A patch is added to clean up the TEE_SHM_* flags
* Fixed a warning reported by kernel test robot <[email protected]>
Jens Wiklander (10):
hwrng: optee-rng: use tee_shm_alloc_kernel_buf()
tee: remove unused tee_shm_pool_alloc_res_mem()
tee: add tee_shm_alloc_user_buf()
tee: simplify shm pool handling
tee: replace tee_shm_alloc()
optee: add optee_pool_op_free_helper()
tee: add tee_shm_register_{user,kernel}_buf()
KEYS: trusted: tee: use tee_shm_register_kernel_buf()
tee: replace tee_shm_register()
tee: refactor TEE_SHM_* flags
drivers/char/hw_random/optee-rng.c | 6 +- drivers/tee/amdtee/shm_pool.c | 55 ++-- drivers/tee/optee/Kconfig | 8 - drivers/tee/optee/call.c | 2 +- drivers/tee/optee/core.c | 21 +- drivers/tee/optee/device.c | 5 +- drivers/tee/optee/ffa_abi.c | 63 ++--- drivers/tee/optee/optee_private.h | 7 +- drivers/tee/optee/smc_abi.c | 125 +++------ drivers/tee/tee_core.c | 5 +- drivers/tee/tee_private.h | 15 +- drivers/tee/tee_shm.c | 320 +++++++++++++++-------- drivers/tee/tee_shm_pool.c | 162 +++--------- include/linux/tee_drv.h | 138 +++------- security/keys/trusted-keys/trusted_tee.c | 23 +- 15 files changed, 388 insertions(+), 567 deletions(-)
Signature · 1 line
2.31.1
PATCH v4 1/10 hwrng: optee-rng: use tee_shm_alloc_kernel_buf()
Uses the new simplified tee_shm_alloc_kernel_buf() function instead of the old deprecated tee_shm_alloc() function which required specific TEE_SHM-flags.
--- drivers/char/hw_random/optee-rng.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/char/hw_random/optee-rng.c +3 -3
@@ -145,10 +145,10 @@ static int optee_rng_init(struct hwrng *rng)struct optee_rng_private *pvt_data =to_optee_rng_private (rng);struct tee_shm *entropy_shm_pool =NULL ; - entropy_shm_pool =tee_shm_alloc (pvt_data->ctx,MAX_ENTROPY_REQ_SZ , -TEE_SHM_MAPPED |TEE_SHM_DMA_BUF ); + entropy_shm_pool =tee_shm_alloc_kernel_buf (pvt_data->ctx, +MAX_ENTROPY_REQ_SZ );if (IS_ERR (entropy_shm_pool)) { -dev_err (pvt_data->dev,"tee_shm_alloc failed\n" ); +dev_err (pvt_data->dev,"tee_shm_alloc_kernel_buf failed\n" );return PTR_ERR (entropy_shm_pool); }
Signature · 1 line
2.31.1
Re: [PATCH v4 01/10] hwrng: optee-rng: use tee_shm_alloc_kernel_buf()
Hi Herbert,
Quoted 10 lines · Jens Wiklander
Uses the new simplified
tee_shm_alloc_kernel_buf()function instead of the old deprecatedtee_shm_alloc()function which required specific TEE_SHM-flags.Reviewed-by Sumit Garg <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- drivers/char/hw_random/optee-rng.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Since this patch depends on other patches in this patch set and vice versa is it OK if I take this patch via my tree? I'm aiming for v5.18 with this.
Thanks,
Jens
Quoted 22 lines
drivers/char/hw_random/optee-rng.c +3
-4@@ -145,10 +145,10 @@ static int optee_rng_init(struct hwrng *rng)struct optee_rng_private *pvt_data =to_optee_rng_private (rng);struct tee_shm *entropy_shm_pool =NULL ; - entropy_shm_pool =tee_shm_alloc (pvt_data->ctx,MAX_ENTROPY_REQ_SZ , -TEE_SHM_MAPPED |TEE_SHM_DMA_BUF ); + entropy_shm_pool =tee_shm_alloc_kernel_buf (pvt_data->ctx, +MAX_ENTROPY_REQ_SZ );if (IS_ERR (entropy_shm_pool)) { -dev_err (pvt_data->dev,"tee_shm_alloc failed\n" ); +dev_err (pvt_data->dev,"tee_shm_alloc_kernel_buf failed\n" );return PTR_ERR (entropy_shm_pool); } --2.31.1
PATCH v4 2/10 tee: remove unused tee_shm_pool_alloc_res_mem()
In reply to Jens WiklanderNone of the drivers in the TEE subsystem uses tee_shm_pool_alloc_res_mem() so remove the function.
--- drivers/tee/tee_shm_pool.c | 56 -------------------------------------- include/linux/tee_drv.h | 30 -------------------- 2 files changed, 86 deletions(-)
drivers/tee/tee_shm_pool.c +0 -56
@@ -47,62 +47,6 @@ static const struct tee_shm_pool_mgr_ops pool_ops_generic = { .destroy_poolmgr = pool_op_gen_destroy_poolmgr, }; -/** -* tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved -* memory range -* @priv_info: Information for driver private shared memory pool -* @dmabuf_info: Information for dma-buf shared memory pool -* -* Start and end of pools will must be page aligned. -* -* Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied -* in @dmabuf, others will use the range provided by @priv. -* -* @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. -*/ -struct tee_shm_pool * -tee_shm_pool_alloc_res_mem (struct tee_shm_pool_mem_info *priv_info, -struct tee_shm_pool_mem_info *dmabuf_info) -{ -struct tee_shm_pool_mgr *priv_mgr; -struct tee_shm_pool_mgr *dmabuf_mgr; -void *rc; - -/* -* Create the pool for driver private shared memory -*/ - rc =tee_shm_pool_mgr_alloc_res_mem (priv_info->vaddr, priv_info->paddr, - priv_info->size, -3 /* 8 byte aligned */ ); -if (IS_ERR (rc)) -return rc; - priv_mgr = rc; - -/* -* Create the pool for dma_buf shared memory -*/ - rc =tee_shm_pool_mgr_alloc_res_mem (dmabuf_info->vaddr, - dmabuf_info->paddr, - dmabuf_info->size,PAGE_SHIFT ); -if (IS_ERR (rc)) - goto err_free_priv_mgr; - dmabuf_mgr = rc; - - rc =tee_shm_pool_alloc (priv_mgr, dmabuf_mgr); -if (IS_ERR (rc)) - goto err_free_dmabuf_mgr; - -return rc; - -err_free_dmabuf_mgr: -tee_shm_pool_mgr_destroy (dmabuf_mgr); -err_free_priv_mgr: -tee_shm_pool_mgr_destroy (priv_mgr); - -return rc; -} -EXPORT_SYMBOL_GPL (tee_shm_pool_alloc_res_mem); - struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr, phys_addr_t paddr, size_t size,
include/linux/tee_drv.h +0 -30
@@ -278,36 +278,6 @@ static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm) poolm->ops->destroy_poolmgr (poolm); } -/** -* struct tee_shm_pool_mem_info - holds information needed to create a shared -* memory pool -* @vaddr: Virtual address of start of pool -* @paddr: Physical address of start of pool -* @size: Size in bytes of the pool -*/ -struct tee_shm_pool_mem_info { -unsigned long vaddr; -phys_addr_t paddr; -size_t size; -}; - -/** -* tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved -* memory range -* @priv_info: Information for driver private shared memory pool -* @dmabuf_info: Information for dma-buf shared memory pool -* -* Start and end of pools will must be page aligned. -* -* Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied -* in @dmabuf, others will use the range provided by @priv. -* -* @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. -*/ -struct tee_shm_pool * -tee_shm_pool_alloc_res_mem (struct tee_shm_pool_mem_info *priv_info, -struct tee_shm_pool_mem_info *dmabuf_info); -/** * tee_shm_pool_free() - Free a shared memory pool * @pool: The shared memory pool to free
Signature · 1 line
2.31.1
PATCH v4 3/10 tee: add tee_shm_alloc_user_buf()
In reply to Jens WiklanderAdds a new function tee_shm_alloc_user_buf() for user mode allocations, replacing passing the flags TEE_SHM_MAPPED | TEE_SHM_DMA_BUF to tee_shm_alloc().
--- drivers/tee/tee_core.c | 2 +- drivers/tee/tee_private.h | 2 ++ drivers/tee/tee_shm.c | 17 +++++++++++++++++ drivers/tee/tee_shm_pool.c | 2 +- include/linux/tee_drv.h | 2 +- 5 files changed, 22 insertions(+), 3 deletions(-)
drivers/tee/tee_core.c +1 -1
@@ -297,7 +297,7 @@ static int tee_ioctl_shm_alloc(struct tee_context *ctx,if (data.flags)return -EINVAL ; - shm =tee_shm_alloc (ctx, data.size,TEE_SHM_MAPPED |TEE_SHM_DMA_BUF ); + shm =tee_shm_alloc_user_buf (ctx, data.size);if (IS_ERR (shm))return PTR_ERR (shm);
drivers/tee/tee_private.h +2 -0
@@ -68,4 +68,6 @@ void tee_device_put(struct tee_device *teedev);void teedev_ctx_get (struct tee_context *ctx);void teedev_ctx_put (struct tee_context *ctx); +struct tee_shm *tee_shm_alloc_user_buf (struct tee_context *ctx,size_t size); +#endif /*TEE_PRIVATE_H*/
drivers/tee/tee_shm.c +17 -0
@@ -127,6 +127,23 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) }EXPORT_SYMBOL_GPL (tee_shm_alloc); +/** +* tee_shm_alloc_user_buf() - Allocate shared memory for user space +* @ctx: Context that allocates the shared memory +* @size: Requested size of shared memory +* +* Memory allocated as user space shared memory is automatically freed when +* the TEE file pointer is closed. The primary usage of this function is +* when the TEE driver doesn't support registering ordinary user space +* memory. +* +* @returns a pointer to 'struct tee_shm' +*/ +struct tee_shm *tee_shm_alloc_user_buf (struct tee_context *ctx,size_t size) +{ +return tee_shm_alloc (ctx, size,TEE_SHM_MAPPED |TEE_SHM_DMA_BUF ); +} +/** * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer * @ctx: Context that allocates the shared memory
drivers/tee/tee_shm_pool.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* -* Copyright (c) 2015, Linaro Limited +* Copyright (c) 2015 Linaro Limited */ #include <linux/device.h> #include <linux/dma-buf.h>
include/linux/tee_drv.h +1 -1
@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-only */ /* -* Copyright (c) 2015-2016, Linaro Limited +* Copyright (c) 2015-2016 Linaro Limited */ #ifndef __TEE_DRV_H
Signature · 1 line
2.31.1
PATCH v4 4/10 tee: simplify shm pool handling
In reply to Jens WiklanderReplaces the shared memory pool based on two pools with a single pool. The alloc() function pointer in struct tee_shm_pool_ops gets another parameter, align. This makes it possible to make less than page aligned allocations from the optional reserved shared memory pool while still making user space allocations page aligned. With in practice unchanged behaviour using only a single pool for bookkeeping.
The allocation algorithm in the static OP-TEE shared memory pool is changed from best-fit to first-fit since only the latter supports an alignment parameter. The best-fit algorithm was previously the default choice and not a conscious one.
The optee and amdtee drivers are updated as needed to work with this changed pool handling.
This also removes OPTEE_SHM_NUM_PRIV_PAGES which becomes obsolete with this change as the private pages can be mixed with the payload pages.
The OP-TEE driver changes minimum alignment for argument struct from 8 bytes to 512 bytes. A typical OP-TEE private shm allocation is 224 bytes (argument struct with 6 parameters, needed for open session). So with an alignment of 512 well waste a bit more than 50%. Before this we had a single page reserved for this so worst case usage compared to that would be 3 pages instead of 1 page. However, this worst case only occurs if there is a high pressure from multiple threads on secure world. All in all this should scale up and down better than fixed boundaries.
--- drivers/tee/amdtee/shm_pool.c | 55 +++++---------- drivers/tee/optee/Kconfig | 8 --- drivers/tee/optee/core.c | 11 +-- drivers/tee/optee/ffa_abi.c | 55 ++++----------- drivers/tee/optee/optee_private.h | 4 +- drivers/tee/optee/smc_abi.c | 108 +++++++++--------------------- drivers/tee/tee_private.h | 11 --- drivers/tee/tee_shm.c | 29 ++++---- drivers/tee/tee_shm_pool.c | 106 ++++++++++------------------- include/linux/tee_drv.h | 60 ++++++----------- 10 files changed, 137 insertions(+), 310 deletions(-)
drivers/tee/amdtee/shm_pool.c +16 -39
@@ -8,13 +8,17 @@#include <linux/psp-sev.h> #include "amdtee_private.h" -static int pool_op_alloc (struct tee_shm_pool_mgr *poolm,struct tee_shm *shm, -size_t size) +static int pool_op_alloc (struct tee_shm_pool *pool,struct tee_shm *shm, +size_t size,size_t align) {unsigned int order =get_order (size);unsigned long va;int rc; +/* +* Ignore alignment since this is already going to be page aligned +* and there's no need for any larger alignment. +*/ va =__get_free_pages (GFP_KERNEL | __GFP_ZERO, order);if (!va)return -ENOMEM ;
@@ -34,7 +38,7 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, struct tee_shm *shm,return 0 ; } -static void pool_op_free (struct tee_shm_pool_mgr *poolm,struct tee_shm *shm) +static void pool_op_free (struct tee_shm_pool *pool,struct tee_shm *shm) {/* Unmap the shared memory from TEE */ amdtee_unmap_shmem (shm);
@@ -42,52 +46,25 @@ static void pool_op_free(struct tee_shm_pool_mgr *poolm, struct tee_shm *shm) shm->kaddr =NULL ; } -static void pool_op_destroy_poolmgr (struct tee_shm_pool_mgr *poolm) +static void pool_op_destroy_pool (struct tee_shm_pool *pool) { -kfree (poolm); +kfree (pool); } -static const struct tee_shm_pool_mgr_ops pool_ops = { +static const struct tee_shm_pool_ops pool_ops = { .alloc = pool_op_alloc, .free = pool_op_free, - .destroy_poolmgr = pool_op_destroy_poolmgr, + .destroy_pool = pool_op_destroy_pool, }; -static struct tee_shm_pool_mgr *pool_mem_mgr_alloc (void ) -{ -struct tee_shm_pool_mgr *mgr =kzalloc (sizeof (*mgr),GFP_KERNEL ); - -if (!mgr) -return ERR_PTR (-ENOMEM ); - - mgr->ops = &pool_ops; - -return mgr; -} -struct tee_shm_pool *amdtee_config_shm (void ) { -struct tee_shm_pool_mgr *priv_mgr; -struct tee_shm_pool_mgr *dmabuf_mgr; -void *rc; +struct tee_shm_pool *pool =kzalloc (sizeof (*pool),GFP_KERNEL ); - rc =pool_mem_mgr_alloc (); -if (IS_ERR (rc)) -return rc; - priv_mgr = rc; - - rc =pool_mem_mgr_alloc (); -if (IS_ERR (rc)) { -tee_shm_pool_mgr_destroy (priv_mgr); -return rc; - } - dmabuf_mgr = rc; +if (!pool) +return ERR_PTR (-ENOMEM ); - rc =tee_shm_pool_alloc (priv_mgr, dmabuf_mgr); -if (IS_ERR (rc)) { -tee_shm_pool_mgr_destroy (priv_mgr); -tee_shm_pool_mgr_destroy (dmabuf_mgr); - } + pool->ops = &pool_ops; -return rc; +return pool; }
drivers/tee/optee/Kconfig +0 -8
@@ -7,11 +7,3 @@ config OPTEE help This implements the OP-TEE Trusted Execution Environment (TEE) driver. - -config OPTEE_SHM_NUM_PRIV_PAGES - int "Private Shared Memory Pages" - default 1 - depends on OPTEE - help - This sets the number of private shared memory pages to be - used by OP-TEE TEE driver.
drivers/tee/optee/core.c +7 -4
@@ -18,8 +18,8 @@#include <linux/workqueue.h> #include "optee_private.h" -int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size, +int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, struct page **pages,
@@ -30,6 +30,10 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm,struct page *page;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)return -ENOMEM ;
@@ -51,7 +55,6 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm,for (i =0 ; i < nr_pages; i++) pages[i] = page + i; - shm->flags |=TEE_SHM_REGISTER ; rc =shm_register (shm->ctx, shm, pages, nr_pages, (unsigned long )shm->kaddr);kfree (pages);
@@ -62,7 +65,7 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm,return 0 ; err: -__free_pages (page, order); +free_pages ((unsigned long )shm->kaddr, order);return rc; }
drivers/tee/optee/ffa_abi.c +14 -41
99 lines not on this page · raw message
drivers/tee/optee/optee_private.h +2 -2
@@ -229,8 +229,8 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);int optee_enumerate_devices (u32 func);void optee_unregister_devices (void ); -int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size, +int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, struct page **pages,
drivers/tee/optee/smc_abi.c +30 -78
184 lines not on this page · raw message
drivers/tee/tee_private.h +0 -11
@@ -12,17 +12,6 @@#include <linux/mutex.h> #include <linux/types.h> -/** -* struct tee_shm_pool - shared memory pool -* @private_mgr: pool manager for shared memory only between kernel -* and secure world -* @dma_buf_mgr: pool manager for shared memory exported to user space -*/ -struct tee_shm_pool { -struct tee_shm_pool_mgr *private_mgr; -struct tee_shm_pool_mgr *dma_buf_mgr; -}; -#define TEE_DEVICE_FLAG_REGISTERED 0x1#define TEE_MAX_DEV_NAME_LEN 32
drivers/tee/tee_shm.c +14 -15
@@ -31,14 +31,7 @@ static void release_registered_pages(struct tee_shm *shm)static void tee_shm_release (struct tee_device *teedev,struct tee_shm *shm) {if (shm->flags &TEE_SHM_POOL ) { -struct tee_shm_pool_mgr *poolm; - -if (shm->flags &TEE_SHM_DMA_BUF ) - poolm = teedev->pool->dma_buf_mgr; -else - poolm = teedev->pool->private_mgr; - - poolm->ops->free (poolm, shm); + teedev->pool->ops->free (teedev->pool, shm); }else if (shm->flags &TEE_SHM_REGISTER ) {int rc = teedev->desc->ops->shm_unregister (shm->ctx, shm);
@@ -59,8 +52,8 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)struct tee_shm *tee_shm_alloc (struct tee_context *ctx,size_t size,u32 flags) {struct tee_device *teedev = ctx->teedev; -struct tee_shm_pool_mgr *poolm =NULL ;struct tee_shm *shm; +size_t align;void *ret;int rc;
@@ -93,12 +86,18 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)refcount_set (&shm->refcount,1 ); shm->flags = flags |TEE_SHM_POOL ; shm->ctx = ctx; -if (flags &TEE_SHM_DMA_BUF ) - poolm = teedev->pool->dma_buf_mgr; -else - poolm = teedev->pool->private_mgr; +if (flags &TEE_SHM_DMA_BUF ) { + align =PAGE_SIZE ; +/* +* Request to register the shm in the pool allocator below +* if supported. +*/ + shm->flags |=TEE_SHM_REGISTER ; + }else { + align =2 *sizeof (long ); + } - rc = poolm->ops->alloc (poolm, shm, size); + rc = teedev->pool->ops->alloc (teedev->pool, shm, size, align);if (rc) { ret =ERR_PTR (rc); goto err_kfree;
@@ -118,7 +117,7 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)return shm; err_pool_free: - poolm->ops->free (poolm, shm); + teedev->pool->ops->free (teedev->pool, shm); err_kfree:kfree (shm); err_dev_put:
drivers/tee/tee_shm_pool.c +34 -72
161 lines not on this page · raw message
include/linux/tee_drv.h +20 -40
95 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 5/10 tee: replace tee_shm_alloc()
In reply to Jens Wiklandertee_shm_alloc() is replaced by three new functions,
tee_shm_alloc_user_buf() - for user mode allocations, replacing passing the flags TEE_SHM_MAPPED | TEE_SHM_DMA_BUF
tee_shm_alloc_kernel_buf() - for kernel mode allocations, slightly optimized compared to using the flags TEE_SHM_MAPPED | TEE_SHM_DMA_BUF.
tee_shm_alloc_priv_buf() - primarily for TEE driver internal use.
This also makes the interface easier to use as we can get rid of the somewhat hard to use flags parameter.
The TEE subsystem and the TEE drivers are updated to use the new functions instead.
--- drivers/tee/optee/call.c | 2 +- drivers/tee/optee/device.c | 5 +- drivers/tee/optee/ffa_abi.c | 4 +- drivers/tee/optee/smc_abi.c | 6 +- drivers/tee/tee_shm.c | 108 +++++++++++++++++++++++------------- include/linux/tee_drv.h | 16 +----- 6 files changed, 76 insertions(+), 65 deletions(-)
drivers/tee/optee/call.c +1 -1
@@ -120,7 +120,7 @@ struct tee_shm *optee_get_msg_arg(struct tee_context *ctx, size_t num_params,if (optee->rpc_arg_count) sz +=OPTEE_MSG_GET_ARG_SIZE (optee->rpc_arg_count); - shm =tee_shm_alloc (ctx, sz,TEE_SHM_MAPPED |TEE_SHM_PRIV ); + shm =tee_shm_alloc_priv_buf (ctx, sz);if (IS_ERR (shm))return shm;
drivers/tee/optee/device.c +2 -3
@@ -121,10 +121,9 @@ static int __optee_enumerate_devices(u32 func)if (rc <0 || !shm_size) goto out_sess; - device_shm =tee_shm_alloc (ctx, shm_size, -TEE_SHM_MAPPED |TEE_SHM_DMA_BUF ); + device_shm =tee_shm_alloc_kernel_buf (ctx, shm_size);if (IS_ERR (device_shm)) { -pr_err ("tee_shm_alloc failed\n" ); +pr_err ("tee_shm_alloc_kernel_buf failed\n" ); rc =PTR_ERR (device_shm); goto out_sess; }
drivers/tee/optee/ffa_abi.c +2 -2
@@ -440,8 +440,8 @@ static void handle_ffa_rpc_func_cmd_shm_alloc(struct tee_context *ctx, shm =optee_rpc_cmd_alloc_suppl (ctx, arg->params[0 ].u.value.b);break ;case OPTEE_RPC_SHM_TYPE_KERNEL : - shm =tee_shm_alloc (optee->ctx, arg->params[0 ].u.value.b, -TEE_SHM_MAPPED |TEE_SHM_PRIV ); + shm =tee_shm_alloc_priv_buf (optee->ctx, + arg->params[0 ].u.value.b);break ;default : arg->ret =TEEC_ERROR_BAD_PARAMETERS ;
drivers/tee/optee/smc_abi.c +2 -4
@@ -651,8 +651,7 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx, shm =optee_rpc_cmd_alloc_suppl (ctx, sz);break ;case OPTEE_RPC_SHM_TYPE_KERNEL : - shm =tee_shm_alloc (optee->ctx, sz, -TEE_SHM_MAPPED |TEE_SHM_PRIV ); + shm =tee_shm_alloc_priv_buf (optee->ctx, sz);break ;default : arg->ret =TEEC_ERROR_BAD_PARAMETERS ;
@@ -777,8 +776,7 @@ static void optee_handle_rpc(struct tee_context *ctx,switch (OPTEE_SMC_RETURN_GET_RPC_FUNC (param->a0)) {case OPTEE_SMC_RPC_FUNC_ALLOC : - shm =tee_shm_alloc (optee->ctx, param->a1, -TEE_SHM_MAPPED |TEE_SHM_PRIV ); + shm =tee_shm_alloc_priv_buf (optee->ctx, param->a1);if (!IS_ERR (shm) && !tee_shm_get_pa (shm,0 , &pa)) {reg_pair_from_64 (¶m->a1, ¶m->a2, pa); reg_pair_from_64(¶m->a4, ¶m->a5,
drivers/tee/tee_shm.c +68 -40
156 lines not on this page · raw message
include/linux/tee_drv.h +1 -15
@@ -273,21 +273,7 @@ static inline void tee_shm_pool_free(struct tee_shm_pool *pool)*/ void *tee_get_drvdata (struct tee_device *teedev); -/** -* tee_shm_alloc() - Allocate shared memory -* @ctx: Context that allocates the shared memory -* @size: Requested size of shared memory -* @flags: Flags setting properties for the requested shared memory. -* -* Memory allocated as global shared memory is automatically freed when the -* TEE file pointer is closed. The @flags field uses the bits defined by -* TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If -* TEE_SHM_DMA_BUF global shared memory will be allocated and associated -* with a dma-buf handle, else driver private memory. -* -* @returns a pointer to 'struct tee_shm' -*/ -struct tee_shm *tee_shm_alloc (struct tee_context *ctx,size_t size,u32 flags); +struct tee_shm *tee_shm_alloc_priv_buf (struct tee_context *ctx,size_t size);struct tee_shm *tee_shm_alloc_kernel_buf (struct tee_context *ctx,size_t size);/**
Signature · 1 line
2.31.1
PATCH v4 6/10 optee: add optee_pool_op_free_helper()
In reply to Jens WiklanderAdds a common helper function to free a tee_shm allocated using the helper function optee_pool_op_alloc_helper().
--- drivers/tee/optee/core.c | 10 ++++++++++ drivers/tee/optee/ffa_abi.c | 4 +--- drivers/tee/optee/optee_private.h | 3 +++ drivers/tee/optee/smc_abi.c | 7 +++---- 4 files changed, 17 insertions(+), 7 deletions(-)
drivers/tee/optee/core.c +10 -0
@@ -69,6 +69,16 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,return rc; } +void optee_pool_op_free_helper (struct tee_shm_pool *pool,struct tee_shm *shm, +int (*shm_unregister)(struct tee_context *ctx, +struct tee_shm *shm)) +{ +if (shm_unregister) +shm_unregister (shm->ctx, shm); +free_pages ((unsigned long )shm->kaddr,get_order (shm->size)); + shm->kaddr =NULL ; +} +static void optee_bus_scan (struct work_struct *work) {WARN_ON (optee_enumerate_devices (PTA_CMD_GET_DEVICES_SUPP ));
drivers/tee/optee/ffa_abi.c +1 -3
@@ -379,9 +379,7 @@ static int pool_ffa_op_alloc(struct tee_shm_pool *pool,static void pool_ffa_op_free (struct tee_shm_pool *pool,struct tee_shm *shm) { -optee_ffa_shm_unregister (shm->ctx, shm); -free_pages ((unsigned long )shm->kaddr,get_order (shm->size)); - shm->kaddr =NULL ; +optee_pool_op_free_helper (pool, shm, optee_ffa_shm_unregister); }static void pool_ffa_op_destroy_pool (struct tee_shm_pool *pool)
drivers/tee/optee/optee_private.h +3 -0
@@ -236,6 +236,9 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,struct page **pages, size_t num_pages, unsigned long start)); +void optee_pool_op_free_helper (struct tee_shm_pool *pool,struct tee_shm *shm, +int (*shm_unregister)(struct tee_context *ctx, +struct tee_shm *shm));void optee_remove_common (struct optee *optee);
drivers/tee/optee/smc_abi.c +3 -4
@@ -548,10 +548,9 @@ static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) {if (!(shm->flags &TEE_SHM_PRIV )) -optee_shm_unregister (shm->ctx, shm); - -free_pages ((unsigned long )shm->kaddr,get_order (shm->size)); - shm->kaddr =NULL ; +optee_pool_op_free_helper (pool, shm, optee_shm_unregister); +else +optee_pool_op_free_helper (pool, shm,NULL ); }static void pool_op_destroy_pool (struct tee_shm_pool *pool)
Signature · 1 line
2.31.1
PATCH v4 7/10 tee: add tee_shm_register_{user,kernel}_buf()
In reply to Jens WiklanderAdds the two new functions tee_shm_register_user_buf() and tee_shm_register_kernel_buf() which should be used instead of the old tee_shm_register().
This avoids having the caller supplying the flags parameter which exposes a bit more than desired of the internals of the TEE subsystem.
--- drivers/tee/tee_core.c | 3 +-- drivers/tee/tee_private.h | 2 ++ drivers/tee/tee_shm.c | 33 +++++++++++++++++++++++++++++++++ include/linux/tee_drv.h | 2 ++ 4 files changed, 38 insertions(+), 2 deletions(-)
drivers/tee/tee_core.c +1 -2
@@ -334,8 +334,7 @@ tee_ioctl_shm_register(struct tee_context *ctx,if (data.flags)return -EINVAL ; - shm =tee_shm_register (ctx, data.addr, data.length, -TEE_SHM_DMA_BUF |TEE_SHM_USER_MAPPED ); + shm =tee_shm_register_user_buf (ctx, data.addr, data.length);if (IS_ERR (shm))return PTR_ERR (shm);
drivers/tee/tee_private.h +2 -0
@@ -58,5 +58,7 @@ void teedev_ctx_get(struct tee_context *ctx);void teedev_ctx_put (struct tee_context *ctx);struct tee_shm *tee_shm_alloc_user_buf (struct tee_context *ctx,size_t size); +struct tee_shm *tee_shm_register_user_buf (struct tee_context *ctx, +unsigned long addr,size_t length);#endif /*TEE_PRIVATE_H*/
drivers/tee/tee_shm.c +33 -0
@@ -299,6 +299,39 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, }EXPORT_SYMBOL_GPL (tee_shm_register); +/** +* tee_shm_register_user_buf() - Register a userspace shared memory buffer +* @ctx: Context that registers the shared memory +* @addr: The userspace address of the shared buffer +* @length: Length of the shared buffer +* +* @returns a pointer to 'struct tee_shm' +*/ +struct tee_shm *tee_shm_register_user_buf (struct tee_context *ctx, +unsigned long addr,size_t length) +{ +return tee_shm_register (ctx, addr, length, +TEE_SHM_DMA_BUF |TEE_SHM_USER_MAPPED ); +} + +/** +* tee_shm_register_kernel_buf() - Register kernel memory to be shared with +* secure world +* @ctx: Context that registers the shared memory +* @addr: The buffer +* @length: Length of the buffer +* +* @returns a pointer to 'struct tee_shm' +*/ + +struct tee_shm *tee_shm_register_kernel_buf (struct tee_context *ctx, +void *addr,size_t length) +{ +return tee_shm_register (ctx, (unsigned long )addr, length, +TEE_SHM_DMA_BUF |TEE_SHM_KERNEL_MAPPED ); +} +EXPORT_SYMBOL_GPL (tee_shm_register_kernel_buf); +static int tee_shm_fop_release (struct inode *inode,struct file *filp) {tee_shm_put (filp->private_data);
include/linux/tee_drv.h +2 -0
@@ -287,6 +287,8 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);*/ struct tee_shm *tee_shm_register (struct tee_context *ctx,unsigned long addr,size_t length,u32 flags); +struct tee_shm *tee_shm_register_kernel_buf (struct tee_context *ctx, +void *addr,size_t length);/** * tee_shm_is_registered() - Check if shared memory object in registered in TEE
Signature · 1 line
2.31.1
PATCH v4 8/10 KEYS: trusted: tee: use tee_shm_register_kernel_buf()
In reply to Jens WiklanderUses the new simplified tee_shm_register_kernel_buf() function instead of the old tee_shm_alloc() function which required specific TEE_SHM-flags
--- security/keys/trusted-keys/trusted_tee.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
security/keys/trusted-keys/trusted_tee.c +9 -14
@@ -70,17 +70,15 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)memset (&inv_arg,0 ,sizeof (inv_arg));memset (¶m,0 ,sizeof (param)); - reg_shm_in =tee_shm_register (pvt_data.ctx, (unsigned long )p->key, - p->key_len,TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_in =tee_shm_register_kernel_buf (pvt_data.ctx, p->key, + p->key_len);if (IS_ERR (reg_shm_in)) {dev_err (pvt_data.dev,"key shm register failed\n" );return PTR_ERR (reg_shm_in); } - reg_shm_out =tee_shm_register (pvt_data.ctx, (unsigned long )p->blob, -sizeof (p->blob),TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_out =tee_shm_register_kernel_buf (pvt_data.ctx, p->blob, +sizeof (p->blob));if (IS_ERR (reg_shm_out)) {dev_err (pvt_data.dev,"blob shm register failed\n" ); ret =PTR_ERR (reg_shm_out);
@@ -131,17 +129,15 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)memset (&inv_arg,0 ,sizeof (inv_arg));memset (¶m,0 ,sizeof (param)); - reg_shm_in =tee_shm_register (pvt_data.ctx, (unsigned long )p->blob, - p->blob_len,TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_in =tee_shm_register_kernel_buf (pvt_data.ctx, p->blob, + p->blob_len);if (IS_ERR (reg_shm_in)) {dev_err (pvt_data.dev,"blob shm register failed\n" );return PTR_ERR (reg_shm_in); } - reg_shm_out =tee_shm_register (pvt_data.ctx, (unsigned long )p->key, -sizeof (p->key),TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_out =tee_shm_register_kernel_buf (pvt_data.ctx, p->key, +sizeof (p->key));if (IS_ERR (reg_shm_out)) {dev_err (pvt_data.dev,"key shm register failed\n" ); ret =PTR_ERR (reg_shm_out);
@@ -192,8 +188,7 @@ static int trusted_tee_get_random(unsigned char *key, size_t key_len)memset (&inv_arg,0 ,sizeof (inv_arg));memset (¶m,0 ,sizeof (param)); - reg_shm =tee_shm_register (pvt_data.ctx, (unsigned long )key, key_len, -TEE_SHM_DMA_BUF |TEE_SHM_KERNEL_MAPPED ); + reg_shm =tee_shm_register_kernel_buf (pvt_data.ctx, key, key_len);if (IS_ERR (reg_shm)) {dev_err (pvt_data.dev,"key shm register failed\n" );return PTR_ERR (reg_shm);
Signature · 1 line
2.31.1
Re: [PATCH v4 08/10] KEYS: trusted: tee: use tee_shm_register_kernel_buf()
Hi Jarkko,
Quoted 10 lines · Jens Wiklander
Uses the new simplified
tee_shm_register_kernel_buf()function instead of the oldtee_shm_alloc()function which required specific TEE_SHM-flagsReviewed-by Sumit Garg <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- security/keys/trusted-keys/trusted_tee.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
Since this patch depends on other patches in this patch set and vice versa is it OK if I take this patch via my tree? I'm aiming for v5.18 with this.
Thanks,
Jens
Quoted 63 lines
security/keys/trusted-keys/trusted_tee.c +9
-15@@ -70,17 +70,15 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob)memset (&inv_arg,0 ,sizeof (inv_arg));memset (¶m,0 ,sizeof (param)); - reg_shm_in =tee_shm_register (pvt_data.ctx, (unsigned long )p->key, - p->key_len,TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_in =tee_shm_register_kernel_buf (pvt_data.ctx, p->key, + p->key_len);if (IS_ERR (reg_shm_in)) {dev_err (pvt_data.dev,"key shm register failed\n" );return PTR_ERR (reg_shm_in); } - reg_shm_out =tee_shm_register (pvt_data.ctx, (unsigned long )p->blob, -sizeof (p->blob),TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_out =tee_shm_register_kernel_buf (pvt_data.ctx, p->blob, +sizeof (p->blob));if (IS_ERR (reg_shm_out)) {dev_err (pvt_data.dev,"blob shm register failed\n" ); ret =PTR_ERR (reg_shm_out);@@ -131,17 +129,15 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob)memset (&inv_arg,0 ,sizeof (inv_arg));memset (¶m,0 ,sizeof (param)); - reg_shm_in =tee_shm_register (pvt_data.ctx, (unsigned long )p->blob, - p->blob_len,TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_in =tee_shm_register_kernel_buf (pvt_data.ctx, p->blob, + p->blob_len);if (IS_ERR (reg_shm_in)) {dev_err (pvt_data.dev,"blob shm register failed\n" );return PTR_ERR (reg_shm_in); } - reg_shm_out =tee_shm_register (pvt_data.ctx, (unsigned long )p->key, -sizeof (p->key),TEE_SHM_DMA_BUF | -TEE_SHM_KERNEL_MAPPED ); + reg_shm_out =tee_shm_register_kernel_buf (pvt_data.ctx, p->key, +sizeof (p->key));if (IS_ERR (reg_shm_out)) {dev_err (pvt_data.dev,"key shm register failed\n" ); ret =PTR_ERR (reg_shm_out);@@ -192,8 +188,7 @@ static int trusted_tee_get_random(unsigned char *key, size_t key_len)memset (&inv_arg,0 ,sizeof (inv_arg));memset (¶m,0 ,sizeof (param)); - reg_shm =tee_shm_register (pvt_data.ctx, (unsigned long )key, key_len, -TEE_SHM_DMA_BUF |TEE_SHM_KERNEL_MAPPED ); + reg_shm =tee_shm_register_kernel_buf (pvt_data.ctx, key, key_len);if (IS_ERR (reg_shm)) {dev_err (pvt_data.dev,"key shm register failed\n" );return PTR_ERR (reg_shm); --2.31.1
Re: [PATCH v4 08/10] KEYS: trusted: tee: use tee_shm_register_kernel_buf()
Quoted 15 lines · Jens Wiklander
Hi Jarkko,
Quoted 10 lines · Jens Wiklander
Uses the new simplified
tee_shm_register_kernel_buf()function instead of the oldtee_shm_alloc()function which required specific TEE_SHM-flagsReviewed-by Sumit Garg <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- security/keys/trusted-keys/trusted_tee.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
Quoted 3 lines
Since this patch depends on other patches in this patch set and vice versa is it OK if I take this patch via my tree? I'm aiming for v5.18 with this.
Yes.
You can also add:
BR, Jarkko
Re: [PATCH v4 08/10] KEYS: trusted: tee: use tee_shm_register_kernel_buf()
Quoted 21 lines · Jarkko Sakkinen
Quoted 15 lines · Jens Wiklander
Hi Jarkko,
Quoted 10 lines · Jens Wiklander
Uses the new simplified
tee_shm_register_kernel_buf()function instead of the oldtee_shm_alloc()function which required specific TEE_SHM-flagsReviewed-by Sumit Garg <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- security/keys/trusted-keys/trusted_tee.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)Quoted 3 lines
Since this patch depends on other patches in this patch set and vice versa is it OK if I take this patch via my tree? I'm aiming for v5.18 with this.
Quoted 7 lines
Yes.
You can also add:
Reviewed-by Jarkko Sakkinen <[email protected]>BR, Jarkko
Thanks. Unfortunately it's a bit late to apply the R-B, I've already sent the pull request to arm-soc and it has been imported into that tree.
Thanks,
Jens
PATCH v4 9/10 tee: replace tee_shm_register()
In reply to Jens Wiklandertee_shm_register() is replaced by the previously introduced functions tee_shm_register_user_buf() and tee_shm_register_kernel_buf().
Since there are not external callers left we can remove tee_shm_register() and refactor the remains.
--- drivers/tee/tee_shm.c | 156 +++++++++++++++++++++++----------------- include/linux/tee_drv.h | 11 --- 2 files changed, 90 insertions(+), 77 deletions(-)
drivers/tee/tee_shm.c +90 -66
@@ -12,17 +12,43 @@#include <linux/uio.h> #include "tee_private.h" +static void shm_put_kernel_pages (struct page **pages,size_t page_count) +{ +size_t n; + +for (n =0 ; n < page_count; n++) +put_page (pages[n]); +} + +static int shm_get_kernel_pages (unsigned long start,size_t page_count, +struct page **pages) +{ +struct kvec *kiov; +size_t n; +int rc; + + kiov =kcalloc (page_count,sizeof (*kiov),GFP_KERNEL ); +if (!kiov) +return -ENOMEM ; + +for (n =0 ; n < page_count; n++) { + kiov[n].iov_base = (void *)(start + n *PAGE_SIZE ); + kiov[n].iov_len =PAGE_SIZE ; + } + + rc =get_kernel_pages (kiov, page_count,0 , pages); +kfree (kiov); + +return rc; +} +static void release_registered_pages (struct tee_shm *shm) {if (shm->pages) { -if (shm->flags &TEE_SHM_USER_MAPPED ) { +if (shm->flags &TEE_SHM_USER_MAPPED )unpin_user_pages (shm->pages, shm->num_pages); - }else { -size_t n; - -for (n =0 ; n < shm->num_pages; n++) -put_page (shm->pages[n]); - } +else +shm_put_kernel_pages (shm->pages, shm->num_pages);kfree (shm->pages); }
@@ -189,28 +215,24 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size) }EXPORT_SYMBOL_GPL (tee_shm_alloc_priv_buf); -struct tee_shm *tee_shm_register (struct tee_context *ctx,unsigned long addr, -size_t length,u32 flags) +static struct tee_shm * +register_shm_helper (struct tee_context *ctx,unsigned long addr, +size_t length,u32 flags,int id) {struct tee_device *teedev = ctx->teedev; -const u32 req_user_flags =TEE_SHM_DMA_BUF |TEE_SHM_USER_MAPPED ; -const u32 req_kernel_flags =TEE_SHM_DMA_BUF |TEE_SHM_KERNEL_MAPPED ;struct tee_shm *shm; +unsigned long start; +size_t num_pages;void *ret;int rc; -int num_pages; -unsigned long start; - -if (flags != req_user_flags && flags != req_kernel_flags) -return ERR_PTR (-ENOTSUPP );if (!tee_device_get (teedev))return ERR_PTR (-EINVAL );if (!teedev->desc->ops->shm_register || !teedev->desc->ops->shm_unregister) { -tee_device_put (teedev); -return ERR_PTR (-ENOTSUPP ); + ret =ERR_PTR (-ENOTSUPP ); + goto err_dev_put; }teedev_ctx_get (ctx);
@@ -218,13 +240,13 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, shm =kzalloc (sizeof (*shm),GFP_KERNEL );if (!shm) { ret =ERR_PTR (-ENOMEM ); - goto err; + goto err_ctx_put; }refcount_set (&shm->refcount,1 ); - shm->flags = flags |TEE_SHM_REGISTER ; + shm->flags = flags; shm->ctx = ctx; - shm->id =-1 ; + shm->id = id; addr =untagged_addr (addr); start =rounddown (addr,PAGE_SIZE ); shm->offset = addr - start;
@@ -233,71 +255,45 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, shm->pages =kcalloc (num_pages,sizeof (*shm->pages),GFP_KERNEL );if (!shm->pages) { ret =ERR_PTR (-ENOMEM ); - goto err; + goto err_free_shm; } -if (flags &TEE_SHM_USER_MAPPED ) { +if (flags &TEE_SHM_USER_MAPPED ) rc =pin_user_pages_fast (start, num_pages,FOLL_WRITE , shm->pages); - }else { -struct kvec *kiov; -int i; - - kiov =kcalloc (num_pages,sizeof (*kiov),GFP_KERNEL ); -if (!kiov) { - ret =ERR_PTR (-ENOMEM ); - goto err; - } - -for (i =0 ; i < num_pages; i++) { - kiov[i].iov_base = (void *)(start + i *PAGE_SIZE ); - kiov[i].iov_len =PAGE_SIZE ; - } - - rc =get_kernel_pages (kiov, num_pages,0 , shm->pages); -kfree (kiov); - } +else + rc =shm_get_kernel_pages (start, num_pages, shm->pages);if (rc >0 ) shm->num_pages = rc;if (rc != num_pages) {if (rc >=0 ) rc = -ENOMEM ; ret =ERR_PTR (rc); - goto err; - } - -mutex_lock (&teedev->mutex); - shm->id =idr_alloc (&teedev->idr, shm,1 ,0 ,GFP_KERNEL ); -mutex_unlock (&teedev->mutex); - -if (shm->id <0 ) { - ret =ERR_PTR (shm->id); - goto err; + goto err_put_shm_pages; } rc = teedev->desc->ops->shm_register (ctx, shm, shm->pages, shm->num_pages, start);if (rc) { ret =ERR_PTR (rc); - goto err; + goto err_put_shm_pages; }return shm; -err: -if (shm) { -if (shm->id >=0 ) { -mutex_lock (&teedev->mutex); -idr_remove (&teedev->idr, shm->id); -mutex_unlock (&teedev->mutex); - } -release_registered_pages (shm); - } +err_put_shm_pages: +if (flags &TEE_SHM_USER_MAPPED ) +unpin_user_pages (shm->pages, shm->num_pages); +else +shm_put_kernel_pages (shm->pages, shm->num_pages); +kfree (shm->pages); +err_free_shm:kfree (shm); +err_ctx_put:teedev_ctx_put (ctx); +err_dev_put:tee_device_put (teedev);return ret; } -EXPORT_SYMBOL_GPL (tee_shm_register);/** * tee_shm_register_user_buf() - Register a userspace shared memory buffer
@@ -310,8 +306,35 @@ EXPORT_SYMBOL_GPL(tee_shm_register);struct tee_shm *tee_shm_register_user_buf (struct tee_context *ctx,unsigned long addr,size_t length) { -return tee_shm_register (ctx, addr, length, -TEE_SHM_DMA_BUF |TEE_SHM_USER_MAPPED ); +u32 flags =TEE_SHM_DMA_BUF |TEE_SHM_USER_MAPPED |TEE_SHM_REGISTER ; +struct tee_device *teedev = ctx->teedev; +struct tee_shm *shm; +void *ret; +int id; + +mutex_lock (&teedev->mutex); + id =idr_alloc (&teedev->idr,NULL ,1 ,0 ,GFP_KERNEL ); +mutex_unlock (&teedev->mutex); +if (id <0 ) +return ERR_PTR (id); + + shm =register_shm_helper (ctx, addr, length, flags, id); +if (IS_ERR (shm)) { +mutex_lock (&teedev->mutex); +idr_remove (&teedev->idr, id); +mutex_unlock (&teedev->mutex); +return shm; + } + +mutex_lock (&teedev->mutex); + ret =idr_replace (&teedev->idr, shm, id); +mutex_unlock (&teedev->mutex); +if (IS_ERR (ret)) { +tee_shm_free (shm); +return ret; + } + +return shm; }/**
@@ -327,8 +350,9 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,struct tee_shm *tee_shm_register_kernel_buf (struct tee_context *ctx,void *addr,size_t length) { -return tee_shm_register (ctx, (unsigned long )addr, length, -TEE_SHM_DMA_BUF |TEE_SHM_KERNEL_MAPPED ); +u32 flags =TEE_SHM_REGISTER |TEE_SHM_KERNEL_MAPPED ; + +return register_shm_helper (ctx, (unsigned long )addr, length, flags,-1 ); }EXPORT_SYMBOL_GPL (tee_shm_register_kernel_buf);
include/linux/tee_drv.h +0 -11
@@ -276,17 +276,6 @@ void *tee_get_drvdata(struct tee_device *teedev);struct tee_shm *tee_shm_alloc_priv_buf (struct tee_context *ctx,size_t size);struct tee_shm *tee_shm_alloc_kernel_buf (struct tee_context *ctx,size_t size); -/** -* tee_shm_register() - Register shared memory buffer -* @ctx: Context that registers the shared memory -* @addr: Address is userspace of the shared buffer -* @length: Length of the shared buffer -* @flags: Flags setting properties for the requested shared memory. -* -* @returns a pointer to 'struct tee_shm' -*/ -struct tee_shm *tee_shm_register (struct tee_context *ctx,unsigned long addr, -size_t length,u32 flags);struct tee_shm *tee_shm_register_kernel_buf (struct tee_context *ctx,void *addr,size_t length);
Signature · 1 line
2.31.1
PATCH v4 10/10 tee: refactor TEE_SHM_* flags
In reply to Jens WiklanderRemoves the redundant TEE_SHM_DMA_BUF, TEE_SHM_EXT_DMA_BUF, TEE_SHM_MAPPED and TEE_SHM_KERNEL_MAPPED flags.
TEE_SHM_REGISTER is renamed to TEE_SHM_DYNAMIC in order to better match its usage.
Assigns new values to the remaining flags to void gaps.
--- drivers/tee/optee/smc_abi.c | 4 ++-- drivers/tee/tee_shm.c | 23 +++++++++++------------ drivers/tee/tee_shm_pool.c | 2 +- include/linux/tee_drv.h | 21 +++++++++------------ 4 files changed, 23 insertions(+), 27 deletions(-)
drivers/tee/optee/smc_abi.c +2 -2
@@ -228,7 +228,7 @@ static int optee_to_msg_param(struct optee *optee,case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT : -if (tee_shm_is_registered (p->u.memref.shm)) +if (tee_shm_is_dynamic (p->u.memref.shm)) rc =to_msg_param_reg_mem (mp, p);else rc =to_msg_param_tmp_mem (mp, p);
@@ -669,7 +669,7 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx, sz =tee_shm_get_size (shm); -if (tee_shm_is_registered (shm)) { +if (tee_shm_is_dynamic (shm)) {struct page **pages;u64 *pages_list;size_t page_num;
drivers/tee/tee_shm.c +11 -12
@@ -58,7 +58,7 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) {if (shm->flags &TEE_SHM_POOL ) { teedev->pool->ops->free (teedev->pool, shm); - }else if (shm->flags &TEE_SHM_REGISTER ) { + }else if (shm->flags &TEE_SHM_DYNAMIC ) {int rc = teedev->desc->ops->shm_unregister (shm->ctx, shm);if (rc)
@@ -139,8 +139,7 @@ static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t size,*/ struct tee_shm *tee_shm_alloc_user_buf (struct tee_context *ctx,size_t size) { -u32 flags =TEE_SHM_MAPPED |TEE_SHM_DMA_BUF |TEE_SHM_REGISTER | -TEE_SHM_POOL ; +u32 flags =TEE_SHM_DYNAMIC |TEE_SHM_POOL ;struct tee_device *teedev = ctx->teedev;struct tee_shm *shm;void *ret;
@@ -185,7 +184,7 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size)*/ struct tee_shm *tee_shm_alloc_kernel_buf (struct tee_context *ctx,size_t size) { -u32 flags =TEE_SHM_MAPPED |TEE_SHM_REGISTER |TEE_SHM_POOL ; +u32 flags =TEE_SHM_DYNAMIC |TEE_SHM_POOL ;return shm_alloc_helper (ctx, size,PAGE_SIZE , flags,-1 ); }
@@ -209,7 +208,7 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);*/ struct tee_shm *tee_shm_alloc_priv_buf (struct tee_context *ctx,size_t size) { -u32 flags =TEE_SHM_MAPPED |TEE_SHM_PRIV |TEE_SHM_POOL ; +u32 flags =TEE_SHM_PRIV |TEE_SHM_POOL ;return shm_alloc_helper (ctx, size,sizeof (long ) *2 , flags,-1 ); }
@@ -306,7 +305,7 @@ register_shm_helper(struct tee_context *ctx, unsigned long addr,struct tee_shm *tee_shm_register_user_buf (struct tee_context *ctx,unsigned long addr,size_t length) { -u32 flags =TEE_SHM_DMA_BUF |TEE_SHM_USER_MAPPED |TEE_SHM_REGISTER ; +u32 flags =TEE_SHM_USER_MAPPED |TEE_SHM_DYNAMIC ;struct tee_device *teedev = ctx->teedev;struct tee_shm *shm;void *ret;
@@ -350,7 +349,7 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,struct tee_shm *tee_shm_register_kernel_buf (struct tee_context *ctx,void *addr,size_t length) { -u32 flags =TEE_SHM_REGISTER |TEE_SHM_KERNEL_MAPPED ; +u32 flags =TEE_SHM_DYNAMIC ;return register_shm_helper (ctx, (unsigned long )addr, length, flags,-1 ); }
@@ -394,7 +393,7 @@ int tee_shm_get_fd(struct tee_shm *shm) {int fd; -if (!(shm->flags &TEE_SHM_DMA_BUF )) +if (shm->id <0 )return -EINVAL ;/* matched by tee_shm_put() in tee_shm_op_release() */
@@ -424,7 +423,7 @@ EXPORT_SYMBOL_GPL(tee_shm_free);*/ int tee_shm_va2pa (struct tee_shm *shm,void *va,phys_addr_t *pa) { -if (!(shm->flags &TEE_SHM_MAPPED )) +if (!shm->kaddr)return -EINVAL ;/* Check that we're in the range of the shm */ if ((char *)va < (char *)shm->kaddr)
@@ -446,7 +445,7 @@ EXPORT_SYMBOL_GPL(tee_shm_va2pa);*/ int tee_shm_pa2va (struct tee_shm *shm,phys_addr_t pa,void **va) { -if (!(shm->flags &TEE_SHM_MAPPED )) +if (!shm->kaddr)return -EINVAL ;/* Check that we're in the range of the shm */ if (pa < shm->paddr)
@@ -474,7 +473,7 @@ EXPORT_SYMBOL_GPL(tee_shm_pa2va);*/ void *tee_shm_get_va (struct tee_shm *shm,size_t offs) { -if (!(shm->flags &TEE_SHM_MAPPED )) +if (!shm->kaddr)return ERR_PTR (-EINVAL );if (offs >= shm->size)return ERR_PTR (-EINVAL );
@@ -549,7 +548,7 @@ void tee_shm_put(struct tee_shm *shm)* the refcount_inc() in tee_shm_get_from_id() never starts * from 0. */ -if (shm->flags &TEE_SHM_DMA_BUF ) +if (shm->id >=0 )idr_remove (&teedev->idr, shm->id); do_release = true; }
drivers/tee/tee_shm_pool.c +1 -1
@@ -30,7 +30,7 @@ static int pool_op_gen_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,* This is from a static shared memory pool so no need to register * each chunk, and no need to unregister later either. */ - shm->flags &= ~TEE_SHM_REGISTER ; + shm->flags &= ~TEE_SHM_DYNAMIC ;return 0 ; }
include/linux/tee_drv.h +9 -12
@@ -20,14 +20,11 @@* specific TEE driver. */ -#define TEE_SHM_MAPPED BIT(0)/* Memory mapped by the kernel */ -#define TEE_SHM_DMA_BUF BIT(1)/* Memory with dma-buf handle */ -#define TEE_SHM_EXT_DMA_BUF BIT(2)/* Memory with dma-buf handle */ -#define TEE_SHM_REGISTER BIT(3)/* Memory registered in secure world */ -#define TEE_SHM_USER_MAPPED BIT(4)/* Memory mapped in user space */ -#define TEE_SHM_POOL BIT(5)/* Memory allocated from pool */ -#define TEE_SHM_KERNEL_MAPPED BIT(6)/* Memory mapped in kernel space */ -#define TEE_SHM_PRIV BIT(7)/* Memory private to TEE driver */ +#define TEE_SHM_DYNAMIC BIT(0)/* Dynamic shared memory registered */ +/* in secure world */ +#define TEE_SHM_USER_MAPPED BIT(1)/* Memory mapped in user space */ +#define TEE_SHM_POOL BIT(2)/* Memory allocated from pool */ +#define TEE_SHM_PRIV BIT(3)/* Memory private to TEE driver */ struct device ;struct tee_device ;
@@ -280,13 +277,13 @@ struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length);/** -* tee_shm_is_registered() - Check if shared memory object in registered in TEE +* tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind * @shm: Shared memory handle -* @returns true if object is registered in TEE +* @returns true if object is dynamic shared memory */ -static inline bool tee_shm_is_registered (struct tee_shm *shm) +static inline bool tee_shm_is_dynamic (struct tee_shm *shm) { -return shm && (shm->flags &TEE_SHM_REGISTER ); +return shm && (shm->flags &TEE_SHM_DYNAMIC ); }/**
Signature · 1 line
2.31.1
Hi all,
Quoted 18 lines · Jens Wiklander
Hi all,
This patchset is a general cleanup of shared memory handling in the TEE subsystem.
Until now has the in-kernel tee clients used
tee_shm_alloc()andtee_shm_register()to share memory with secure world. These two function exposes via a flags parameter a bit more of the internals of the TEE subsystem than one would like. So in order to make things easier are those two functions replaced by few functions which should provide better abstraction.Two in-kernel tee clients are updated to use these new functions.
The shared memory pool handling is simplified, an internal matter for the two TEE drivers OP-TEE and AMDTEE.
Quoted 7 lines
In the v3 review it was suggested [1] to break out "optee: add driver private
tee_context" and "optee: use driver internaltee_contexfor some rpc" into a separate patch to fix to allow those a faster path upstream as they fix reported problems. So this patchset is now rebased on top of those patches separated.This patchset is based on [2] and is also available at [3].
All patches are reviewed and there are no further comments so I'm picking up this now.
Cheers,
Jens
Quoted 75 lines
Thanks,
Jens[1] https://lore.kernel.org/lkml/20220125162938.838382-1-jens.wiklander(a)linaro.org/T/#m9e38c5788b49ed3929276df69fe856b6cbe14dfb
[2] https://git.linaro.org/people/jens.wiklander/linux-tee.git/log/?h=fixes
[3] https://git.linaro.org/people/jens.wiklander/linux-tee.git/log/?h=tee_shm_v4
v3->v4:
* Broke out "optee: add driver privatetee_context" and "optee: use driver internaltee_contexfor some rpc" into a separate patch as that fixes earlier reported issues and deserves a to go into v5.17 and stable trees.
* Rebased on the recent fixes for the OP-TEE driver on top of v5.17-rc2
* All patches are now reviewed by Sumit Garg + some small fixes from the last review
v2->v3:
* Maketee_shm_alloc_user_buf()andtee_shm_register_user_buf()internal and don't export them to the drivers.
* Renametee_shm_alloc_priv_kernel_buf()totee_shm_alloc_priv_buf()
* Adressing comments on variable names and choice of types in "tee: replacetee_shm_register()"
* Adding detailed explaination on alignment in "tee: simplify shm pool handling"
* Added Sumits R-B on a few of the patches
v1->v2:
* The commits three "tee: addtee_shm_alloc_kernel_buf()", "tpm_ftpm_tee: usetee_shm_alloc_kernel_buf()" and
"firmware:tee_bnxt: usetee_shm_alloc_kernel_buf()" has been merged some time ago as part of another patchset.
* Another in-kernel tee client is updated with the commit
"KEYS: trusted: tee: usetee_shm_register_kernel_buf()"
*tee_shm_alloc_anon_kernel_buf()is replaced with an easier to use functiontee_shm_alloc_priv_kernel_buf()andtee_shm_free_anon_kernel_buf()has been dropped.
* A driver internal structtee_contextis used to when doing driver internal calls to secure world.
* Adds patches to replacetee_shm_register()in a similar way as howtee_shm_alloc()is replaced.
* A patch is added to clean up the TEE_SHM_* flags
* Fixed a warning reported by kernel test robot <[email protected]>Jens Wiklander (10):
hwrng: optee-rng: usetee_shm_alloc_kernel_buf()
tee: remove unusedtee_shm_pool_alloc_res_mem()
tee: addtee_shm_alloc_user_buf()
tee: simplify shm pool handling
tee: replacetee_shm_alloc()
optee: addoptee_pool_op_free_helper()
tee: add tee_shm_register_{user,kernel}_buf()
KEYS: trusted: tee: usetee_shm_register_kernel_buf()
tee: replacetee_shm_register()
tee: refactor TEE_SHM_* flagsdrivers/char/hw_random/optee-rng.c | 6 +- drivers/tee/amdtee/shm_pool.c | 55 ++-- drivers/tee/optee/Kconfig | 8 - drivers/tee/optee/call.c | 2 +- drivers/tee/optee/core.c | 21 +- drivers/tee/optee/device.c | 5 +- drivers/tee/optee/ffa_abi.c | 63 ++--- drivers/tee/optee/optee_private.h | 7 +- drivers/tee/optee/smc_abi.c | 125 +++------ drivers/tee/tee_core.c | 5 +- drivers/tee/tee_private.h | 15 +- drivers/tee/tee_shm.c | 320 +++++++++++++++-------- drivers/tee/tee_shm_pool.c | 162 +++--------- include/linux/tee_drv.h | 138 +++------- security/keys/trusted-keys/trusted_tee.c | 23 +- 15 files changed, 388 insertions(+), 567 deletions(-)--
2.31.1