TEE subsystem for protected dma-buf allocations
Subsystems (8)
v8 cover TEE subsystem for protected dma-buf allocations
Hi,
This patch set allocates the protected DMA-bufs from a DMA-heap instantiated from the TEE subsystem.
The TEE subsystem handles the DMA-buf allocations since it is the TEE (OP-TEE, AMD-TEE, TS-TEE, or perhaps a future QTEE) which sets up the protection for the memory used for the DMA-bufs.
The DMA-heap uses a protected memory pool provided by the backend TEE driver, allowing it to choose how to allocate the protected physical memory.
The allocated DMA-bufs must be imported with a new TEE_IOC_SHM_REGISTER_FD before they can be passed as arguments when requesting services from the secure world.
Three use-cases (Secure Video Playback, Trusted UI, and Secure Video Recording) has been identified so far to serve as examples of what can be expected. The use-cases has predefined DMA-heap names, "protected,secure-video", "protected,trusted-ui", and "protected,secure-video-record". The backend driver registers protected memory pools for the use-cases it supports.
Each use-case has it's own protected memory pool since different use-cases requires isolation from different parts of the system. A protected memory pool can be based on a static carveout instantiated while probing the TEE backend driver, or dynamically allocated from CMA and made protected as needed by the TEE.
This can be tested on a RockPi 4B+ with the following steps:
repo init -u https://github.com/jenswi-linaro/manifest.git -m rockpi4.xml \
-b prototype/sdp-v8
repo sync -j8
cd build
make toolchains -j$(nproc)
make all -j$(nproc)
# Copy ../out/rockpi4.img to an SD card and boot the RockPi from that # Connect a monitor to the RockPi
# login and at the prompt:
gst-launch-1.0 videotestsrc ! \
aesenc key=1f9423681beb9a79215820f6bda73d0f \
iv=e9aa8e834d8d70b7e0d254ff670dd718 serialize-iv=true ! \
aesdec key=1f9423681beb9a79215820f6bda73d0f ! \
kmssinkThe aesdec module has been hacked to use an OP-TEE TA to decrypt the stream into protected DMA-bufs which are consumed by the kmssink.
The primitive QEMU tests from previous patch set can be tested on RockPi in the same way with:
xtest --sdp-basic
The primitive test are tested on QEMU with the following steps:
repo init -u https://github.com/jenswi-linaro/manifest.git -m qemu_v8.xml \
-b prototype/sdp-v8
repo sync -j8
cd build
make toolchains -j$(nproc)
make SPMC_AT_EL=1 all -j$(nproc)
make SPMC_AT_EL=1 run-only
# login and at the prompt: xtest --sdp-basic
The SPMC_AT_EL=1 parameter configures the build with FF-A and an SPMC at S-EL1 inside OP-TEE. The parameter can be changed into SPMC_AT_EL=n to test without FF-A using the original SMC ABI instead. Please remember to do %rm -rf ../trusted-firmware-a/build/qemu
for TF-A to be rebuilt properly using the new configuration.
https://optee.readthedocs.io/en/latest/building/prerequisites.html
list dependencies needed to build the above.
The tests are pretty basic, mostly checking that a Trusted Application in the secure world can access and manipulate the memory. There are also some negative tests for out of bounds buffers etc.
Thanks,
Jens
Changes since V7:
* Adding "dma-buf: dma-heap: export declared functions",
"cma: export cma_alloc() and cma_release()", and "dma-contiguous: export dma_contiguous_default_area" to export the symbols needed to keep the TEE subsystem as a load module.
* Removing CONFIG_TEE_DMABUF_HEAP and CONFIG_TEE_CMA since they aren't needed any longer.
* Addressing review comments in "optee: sync secure world ABI headers"
* Better align protected memory pool initialization between the smc-abi and ffa-abi parts of the optee driver.
Changes since V6:
* Restricted memory is now known as protected memory since to use the same term as https://docs.vulkan.org/guide/latest/protected.html. Update all patches to consistently use protected memory.
* In "tee: implement protected DMA-heap" add the hidden config option TEE_DMABUF_HEAP to tell if the DMABUF_HEAPS functions are available for the TEE subsystem
* Adding "tee: refactor params_from_user()", broken out from the patch "tee: new ioctl to a register tee_shm from a dmabuf file descriptor"
* For "tee: new ioctl to a register tee_shm from a dmabuf file descriptor":
- Update commit message to mention protected memory
- Remove and open code tee_shm_get_parent_shm() in param_from_user_memref()
* In "tee: add tee_shm_alloc_cma_phys_mem" add the hidden config option TEE_CMA to tell if the CMA functions are available for the TEE subsystem
* For "tee: tee_device_alloc(): copy dma_mask from parent device" and "optee: pass parent device to tee_device_alloc", added
Changes since V5:
* Removing "tee: add restricted memory allocation" and
"tee: add TEE_IOC_RSTMEM_FD_INFO"
* Adding "tee: implement restricted DMA-heap",
"tee: new ioctl to a register tee_shm from a dmabuf file descriptor", "tee: add tee_shm_alloc_cma_phys_mem()",
"optee: pass parent device to tee_device_alloc()", and
"tee: tee_device_alloc(): copy dma_mask from parent device"
* The two TEE driver OPs "rstmem_alloc()" and "rstmem_free()" are replaced with a struct tee_rstmem_pool abstraction.
* Replaced the the TEE_IOC_RSTMEM_ALLOC user space API with the DMA-heap API
Changes since V4:
* Adding the patch "tee: add TEE_IOC_RSTMEM_FD_INFO" needed by the GStreamer demo
* Removing the dummy CPU access and mmap functions from the dma_buf_ops
* Fixing a compile error in "optee: FF-A: dynamic restricted memory allocation" reported by kernel test robot <[email protected]>
Changes since V3:
* Make the use_case and flags field in struct tee_shm u32's instead of u16's
* Add more description for TEE_IOC_RSTMEM_ALLOC in the header file
* Import namespace DMA_BUF in module tee, reported by lkp(a)intel.com
* Added a note in the commit message for "optee: account for direction while converting parameters" why it's needed
* Factor out dynamic restricted memory allocation from "optee: support restricted memory allocation" into two new commits "optee: FF-A: dynamic restricted memory allocation" and "optee: smc abi: dynamic restricted memory allocation"
* Guard CMA usage with #ifdef CONFIG_CMA, effectively disabling dynamic restricted memory allocate if CMA isn't configured
Changes since the V2 RFC:
* Based on v6.12
* Replaced the flags for SVP and Trusted UID memory with a u32 field with unique id for each use case
* Added dynamic allocation of restricted memory pools
* Added OP-TEE ABI both with and without FF-A for dynamic restricted memory
* Added support for FF-A with FFA_LEND
Changes since the V1 RFC:
* Based on v6.11
* Complete rewrite, replacing the restricted heap with TEE_IOC_RSTMEM_ALLOC
Changes since Olivier's post [2]:
* Based on Yong Wu's post [1] where much of dma-buf handling is done in the generic restricted heap
* Simplifications and cleanup
* New commit message for "dma-buf: heaps: add Linaro restricted dmabuf heap support"
* Replaced the word "secure" with "restricted" where applicable
Etienne Carriere (1):
tee: new ioctl to a register tee_shm from a dmabuf file descriptor
Jens Wiklander (13):
tee: tee_device_alloc(): copy dma_mask from parent device
optee: pass parent device to tee_device_alloc()
optee: account for direction while converting parameters
optee: sync secure world ABI headers
dma-buf: dma-heap: export declared functions
tee: implement protected DMA-heap
tee: refactor params_from_user()
cma: export cma_alloc() and cma_release()
dma-contiguous: export dma_contiguous_default_area
tee: add tee_shm_alloc_cma_phys_mem()
optee: support protected memory allocation
optee: FF-A: dynamic protected memory allocation
optee: smc abi: dynamic protected memory allocation
drivers/dma-buf/dma-heap.c | 3 + drivers/tee/Makefile | 1 + drivers/tee/optee/Makefile | 1 + drivers/tee/optee/call.c | 10 +- drivers/tee/optee/core.c | 1 + drivers/tee/optee/ffa_abi.c | 198 ++++++++++++- drivers/tee/optee/optee_ffa.h | 27 +- drivers/tee/optee/optee_msg.h | 83 +++++- drivers/tee/optee/optee_private.h | 55 +++- drivers/tee/optee/optee_smc.h | 69 ++++- drivers/tee/optee/protmem.c | 330 +++++++++++++++++++++ drivers/tee/optee/rpc.c | 31 +- drivers/tee/optee/smc_abi.c | 191 ++++++++++-- drivers/tee/tee_core.c | 157 +++++++--- drivers/tee/tee_heap.c | 470 ++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 16 + drivers/tee/tee_shm.c | 164 ++++++++++- include/linux/tee_core.h | 70 +++++ include/linux/tee_drv.h | 10 + include/uapi/linux/tee.h | 31 ++ kernel/dma/contiguous.c | 1 + mm/cma.c | 2 + 22 files changed, 1789 insertions(+), 132 deletions(-) create mode 100644 drivers/tee/optee/protmem.c create mode 100644 drivers/tee/tee_heap.c
base-commit: b4432656b36e5cc1d50a1f2dc15357543add530e
Signature · 1 line
2.43.0
PATCH v8 1/14 tee: tee_device_alloc(): copy dma_mask from parent device
If a parent device is supplied to tee_device_alloc(), copy the dma_mask field into the new device. This avoids future warnings when mapping a DMA-buf for the device.
--- drivers/tee/tee_core.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/tee/tee_core.c +2 -0
@@ -922,6 +922,8 @@ struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, teedev->dev.class = &tee_class; teedev->dev.release = tee_release_device; teedev->dev.parent = dev; +if (dev) + teedev->dev.dma_mask = dev->dma_mask; teedev->dev.devt =MKDEV (MAJOR (tee_devt), teedev->id);
Signature · 1 line
2.43.0
Robin Murphy
reviewerRe: [PATCH v8 01/14] tee: tee_device_alloc(): copy dma_mask from parent device
Quoted 3 lines · Jens Wiklander
If a parent device is supplied to
tee_device_alloc(), copy thedma_maskfield into the new device. This avoids future warnings when mapping a DMA-buf for the device.
That also sounds dodgy. If the parent device is the hardware device physically performing the DMA, then that is the device which should be passed to the DMA API. Trying to copy random bits of one device's configuration to another device and hoping it will work is not robust - not only is DMA-relevant information all over the place, including in archdata and/or bus/IOMMU driver-private data, but it can also opens up a whole can of subtle lifecycle issues...
Quoted 11 lines
Signed-off-by Jens Wiklander <[email protected]>Reviewed-by Sumit Garg <[email protected]>--- drivers/tee/tee_core.c | 2 ++ 1 file changed, 2 insertions(+)drivers/tee/tee_core.c +0
-0
drivers/tee/tee_core.c struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, · line 926
Quoted 5 lines
drivers/tee/tee_core.c +2
-0@@ -922,6 +922,8 @@ struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, teedev->dev.class = &tee_class; teedev->dev.release = tee_release_device; teedev->dev.parent = dev; +if (dev) + teedev->dev.dma_mask = dev->dma_mask;
...for instance, I don't see any obvious guarantee that "dev" can't go away during the lifetime of "teedev" and leave this pointer dangling.
Thanks,
Robin.
Quoted 3 lines
drivers/tee/tee_core.c +0
-0teedev->dev.devt =MKDEV (MAJOR (tee_devt), teedev->id);
Re: [PATCH v8 01/14] tee: tee_device_alloc(): copy dma_mask from parent device
Hi,
Quoted 6 lines · Robin Murphy
Quoted 3 lines · Jens Wiklander
If a parent device is supplied to
tee_device_alloc(), copy thedma_maskfield into the new device. This avoids future warnings when mapping a DMA-buf for the device.
Quoted 7 lines
That also sounds dodgy. If the parent device is the hardware device physically performing the DMA, then that is the device which should be passed to the DMA API. Trying to copy random bits of one device's configuration to another device and hoping it will work is not robust - not only is DMA-relevant information all over the place, including in archdata and/or bus/IOMMU driver-private data, but it can also opens up a whole can of subtle lifecycle issues...
We have a reference to the parent device until the teedev goes away. The dma_maks needed by tee_shm_register_fd() in
https://lore.kernel.org/lkml/20250502100049.1746335-9-jens.wiklander(a)linaro.org/
to be able to extract the PA from a DMA-buf allocated from another DMA heap. We can drop this patch and support for unrelated DMA heaps in tee_shm_register_fd() without losing critical features from the patch set if we can't handle dma_mask in this way.
Quoted 7 lines
Quoted 7 lines
Signed-off-by Jens Wiklander <[email protected]>Reviewed-by Sumit Garg <[email protected]>--- drivers/tee/tee_core.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 13 lines
drivers/tee/tee_core.c struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, · line 926
Quoted 10 lines
drivers/tee/tee_core.c +2
-0@@ -922,6 +922,8 @@ struct tee_device *tee_device_alloc(const struct tee_desc *teedesc, teedev->dev.class = &tee_class; teedev->dev.release = tee_release_device; teedev->dev.parent = dev; +if (dev) + teedev->dev.dma_mask = dev->dma_mask;...for instance, I don't see any obvious guarantee that "dev" can't go away during the lifetime of "teedev" and leave this pointer dangling.
A successful call to tee_device_alloc() must be followed by a call to tee_device_register() or tee_device_unregister(). The former calls cdev_device_add(), which results in a call to device_add() and an increased reference to teedev->dev.parent, "dev" in question.
Thanks,
Jens
Quoted 7 lines
Thanks,
Robin.Quoted 3 lines
drivers/tee/tee_core.c +0
-0teedev->dev.devt =MKDEV (MAJOR (tee_devt), teedev->id);
PATCH v8 2/14 optee: pass parent device to tee_device_alloc()
In reply to Jens WiklanderDuring probing of the OP-TEE driver, pass the parent device to tee_device_alloc() so the dma_mask of the new devices can be updated accordingly.
--- drivers/tee/optee/ffa_abi.c | 8 ++++---- drivers/tee/optee/smc_abi.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/tee/optee/ffa_abi.c +4 -4
@@ -914,16 +914,16 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) (sec_caps & OPTEE_FFA_SEC_CAP_RPMB_PROBE)) optee->in_kernel_rpmb_routing = true; - teedev =tee_device_alloc (&optee_ffa_clnt_desc,NULL , optee->pool, - optee); + teedev =tee_device_alloc (&optee_ffa_clnt_desc, &ffa_dev->dev, + optee->pool, optee);if (IS_ERR (teedev)) { rc =PTR_ERR (teedev); goto err_free_pool; } optee->teedev = teedev; - teedev =tee_device_alloc (&optee_ffa_supp_desc,NULL , optee->pool, - optee); + teedev =tee_device_alloc (&optee_ffa_supp_desc, &ffa_dev->dev, + optee->pool, optee);if (IS_ERR (teedev)) { rc =PTR_ERR (teedev); goto err_unreg_teedev;
drivers/tee/optee/smc_abi.c +2 -2
@@ -1691,14 +1691,14 @@ static int optee_probe(struct platform_device *pdev) (sec_caps & OPTEE_SMC_SEC_CAP_RPMB_PROBE)) optee->in_kernel_rpmb_routing = true; - teedev =tee_device_alloc (&optee_clnt_desc,NULL , pool, optee); + teedev =tee_device_alloc (&optee_clnt_desc, &pdev->dev, pool, optee);if (IS_ERR (teedev)) { rc =PTR_ERR (teedev); goto err_free_optee; } optee->teedev = teedev; - teedev =tee_device_alloc (&optee_supp_desc,NULL , pool, optee); + teedev =tee_device_alloc (&optee_supp_desc, &pdev->dev, pool, optee);if (IS_ERR (teedev)) { rc =PTR_ERR (teedev); goto err_unreg_teedev;
Signature · 1 line
2.43.0
PATCH v8 3/14 optee: account for direction while converting parameters
In reply to Jens WiklanderThe OP-TEE backend driver has two internal function pointers to convert between the subsystem type struct tee_param and the OP-TEE type struct optee_msg_param.
The conversion is done from one of the types to the other, which is then involved in some operation and finally converted back to the original type. When converting to prepare the parameters for the operation, all fields must be taken into account, but then converting back, it's enough to update only out-values and out-sizes. So, an update_out parameter is added to the conversion functions to tell if all or only some fields must be copied.
This is needed in a later patch where it might get confusing when converting back in from_msg_param() callback since an allocated restricted SHM can be using the sec_world_id of the used restricted memory pool and that doesn't translate back well.
--- drivers/tee/optee/call.c | 10 ++-- drivers/tee/optee/ffa_abi.c | 43 +++++++++++++---- drivers/tee/optee/optee_private.h | 42 +++++++++++------ drivers/tee/optee/rpc.c | 31 +++++++++---- drivers/tee/optee/smc_abi.c | 76 +++++++++++++++++++++++-------- 5 files changed, 144 insertions(+), 58 deletions(-)
drivers/tee/optee/call.c +6 -4
@@ -400,7 +400,8 @@ int optee_open_session(struct tee_context *ctx,export_uuid (msg_arg->params[1 ].u.octets, &client_uuid); rc = optee->ops->to_msg_param (optee, msg_arg->params +2 , - arg->num_params, param); + arg->num_params, param, + false/*!update_out*/ );if (rc) goto out;
@@ -427,7 +428,8 @@ int optee_open_session(struct tee_context *ctx, }if (optee->ops->from_msg_param (optee, param, arg->num_params, - msg_arg->params +2 )) { + msg_arg->params +2 , + true/*update_out*/ )) { arg->ret =TEEC_ERROR_COMMUNICATION ; arg->ret_origin =TEEC_ORIGIN_COMMS ;/* Close session again to avoid leakage */
@@ -541,7 +543,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, msg_arg->cancel_id = arg->cancel_id; rc = optee->ops->to_msg_param (optee, msg_arg->params, arg->num_params, - param); + param, false/*!update_out*/ );if (rc) goto out;
@@ -551,7 +553,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, }if (optee->ops->from_msg_param (optee, param, arg->num_params, - msg_arg->params)) { + msg_arg->params, true/*update_out*/ )) { msg_arg->ret =TEEC_ERROR_COMMUNICATION ; msg_arg->ret_origin =TEEC_ORIGIN_COMMS ; }
drivers/tee/optee/ffa_abi.c +33 -10
131 lines not on this page · raw message
drivers/tee/optee/optee_private.h +28 -14
61 lines not on this page · raw message
drivers/tee/optee/rpc.c +21 -10
@@ -63,7 +63,7 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, }if (optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params)) + arg->params, false/*!update_out*/ )) goto bad;for (i =0 ; i < arg->num_params; i++) {
@@ -107,7 +107,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, }else { params[3 ].u.value.a = msg.len;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) + arg->num_params, params, + true/*update_out*/ )) arg->ret =TEEC_ERROR_BAD_PARAMETERS ;else arg->ret =TEEC_SUCCESS ;
@@ -188,6 +189,7 @@ static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)static void handle_rpc_supp_cmd (struct tee_context *ctx,struct optee *optee,struct optee_msg_arg *arg) { +bool update_out = false;struct tee_param *params; arg->ret_origin =TEEC_ORIGIN_COMMS ;
@@ -200,15 +202,21 @@ static void handle_rpc_supp_cmd(struct tee_context *ctx, struct optee *optee, }if (optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params)) { + arg->params, update_out)) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ; goto out; } arg->ret =optee_supp_thrd_req (ctx, arg->cmd, arg->num_params, params); +/* +* Special treatment for OPTEE_RPC_CMD_SHM_ALLOC since input is a +* value type, but the output is a memref type. +*/ +if (arg->cmd !=OPTEE_RPC_CMD_SHM_ALLOC ) + update_out = true;if (optee->ops->to_msg_param (optee, arg->params, arg->num_params, - params)) + params, update_out)) arg->ret =TEEC_ERROR_BAD_PARAMETERS ; out:kfree (params);
@@ -270,7 +278,7 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ;
@@ -280,7 +288,8 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee_context *ctx, params[0 ].u.value.b =0 ; params[0 ].u.value.c =0 ;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ; }
@@ -324,7 +333,7 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || params[1 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;
@@ -358,7 +367,8 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_context *ctx, params[0 ].u.value.b = rdev->descr.capacity; params[0 ].u.value.c = rdev->descr.reliable_wr_count;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ; }
@@ -384,7 +394,7 @@ static void handle_rpc_func_rpmb_frames(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT || params[1 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;
@@ -401,7 +411,8 @@ static void handle_rpc_func_rpmb_frames(struct tee_context *ctx, goto out; }if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ; goto out; }
drivers/tee/optee/smc_abi.c +56 -20
207 lines not on this page · raw message
Signature · 1 line
2.43.0
Sumit Garg
reviewerRe: [PATCH v8 03/14] optee: account for direction while converting parameters
Hi Jens,
Quoted 17 lines · Jens Wiklander
The OP-TEE backend driver has two internal function pointers to convert between the subsystem type struct
tee_paramand the OP-TEE type structoptee_msg_param.The conversion is done from one of the types to the other, which is then involved in some operation and finally converted back to the original type. When converting to prepare the parameters for the operation, all fields must be taken into account, but then converting back, it's enough to update only out-values and out-sizes. So, an
update_outparameter is added to the conversion functions to tell if all or only some fields must be copied.This is needed in a later patch where it might get confusing when converting back in
from_msg_param()callback since an allocated restricted SHM can be using thesec_world_idof the used restricted memory pool and that doesn't translate back well.
Quoted 9 lines
Signed-off-by Jens Wiklander <[email protected]>--- drivers/tee/optee/call.c | 10 ++-- drivers/tee/optee/ffa_abi.c | 43 +++++++++++++---- drivers/tee/optee/optee_private.h | 42 +++++++++++------ drivers/tee/optee/rpc.c | 31 +++++++++---- drivers/tee/optee/smc_abi.c | 76 +++++++++++++++++++++++-------- 5 files changed, 144 insertions(+), 58 deletions(-)
Thinking about it a bit more, as you mentioned in v6 that this patch isn't strictly required for this series, can we drop this from this series and rather followup with a separate patch? If you agree then I can give a try on simplifying this patch?
-Sumit
Quoted 587 lines
drivers/tee/optee/call.c +6
-4@@ -400,7 +400,8 @@ int optee_open_session(struct tee_context *ctx,export_uuid (msg_arg->params[1 ].u.octets, &client_uuid); rc = optee->ops->to_msg_param (optee, msg_arg->params +2 , - arg->num_params, param); + arg->num_params, param, + false/*!update_out*/ );if (rc) goto out;@@ -427,7 +428,8 @@ int optee_open_session(struct tee_context *ctx, }if (optee->ops->from_msg_param (optee, param, arg->num_params, - msg_arg->params +2 )) { + msg_arg->params +2 , + true/*update_out*/ )) { arg->ret =TEEC_ERROR_COMMUNICATION ; arg->ret_origin =TEEC_ORIGIN_COMMS ;/* Close session again to avoid leakage */ @@ -541,7 +543,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, msg_arg->cancel_id = arg->cancel_id; rc = optee->ops->to_msg_param (optee, msg_arg->params, arg->num_params, - param); + param, false/*!update_out*/ );if (rc) goto out;@@ -551,7 +553,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, }if (optee->ops->from_msg_param (optee, param, arg->num_params, - msg_arg->params)) { + msg_arg->params, true/*update_out*/ )) { msg_arg->ret =TEEC_ERROR_COMMUNICATION ; msg_arg->ret_origin =TEEC_ORIGIN_COMMS ; }drivers/tee/optee/ffa_abi.c +33
-10@@ -122,15 +122,21 @@ static int optee_shm_rem_ffa_handle(struct optee *optee, u64 global_id)*/ static void from_msg_param_ffa_mem (struct optee *optee,struct tee_param *p, -u32 attr,const struct optee_msg_param *mp) +u32 attr,const struct optee_msg_param *mp, +bool update_out) {struct tee_shm *shm =NULL ;u64 offs_high =0 ;u64 offs_low =0 ; +if (update_out) { +if (attr ==OPTEE_MSG_ATTR_TYPE_FMEM_INPUT ) +return ; + goto out; + } + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -OPTEE_MSG_ATTR_TYPE_FMEM_INPUT ; - p->u.memref.size = mp->u.fmem.size;if (mp->u.fmem.global_id !=OPTEE_MSG_FMEM_INVALID_GLOBAL_ID ) shm =optee_shm_from_ffa_handle (optee, mp->u.fmem.global_id);@@ -141,6 +147,8 @@ static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p, offs_high = mp->u.fmem.offs_high; } p->u.memref.shm_offs = offs_low | offs_high <<32 ; +out: + p->u.memref.size = mp->u.fmem.size; }/** @@ -150,12 +158,14 @@ static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p,* @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters +* @update_out: update parameter for output only * * Returns 0 on success or <0 on failure */ static int optee_ffa_from_msg_param (struct optee *optee,struct tee_param *params,size_t num_params, -const struct optee_msg_param *msg_params) +const struct optee_msg_param *msg_params, +bool update_out) {size_t n;@@ -166,18 +176,20 @@ static int optee_ffa_from_msg_param(struct optee *optee,switch (attr) {case OPTEE_MSG_ATTR_TYPE_NONE : +if (update_out) +break ; p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&p->u,0 ,sizeof (p->u));break ;case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT : -optee_from_msg_param_value (p, attr, mp); +optee_from_msg_param_value (p, attr, mp, update_out);break ;case OPTEE_MSG_ATTR_TYPE_FMEM_INPUT :case OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT :case OPTEE_MSG_ATTR_TYPE_FMEM_INOUT : -from_msg_param_ffa_mem (optee, p, attr, mp); +from_msg_param_ffa_mem (optee, p, attr, mp, update_out);break ;default :return -EINVAL ;@@ -188,10 +200,16 @@ static int optee_ffa_from_msg_param(struct optee *optee, }static int to_msg_param_ffa_mem (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p,bool update_out) {struct tee_shm *shm = p->u.memref.shm; +if (update_out) { +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ) +return 0 ; + goto out; + } + mp->attr =OPTEE_MSG_ATTR_TYPE_FMEM_INPUT + p->attr -TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ;@@ -211,6 +229,7 @@ static int to_msg_param_ffa_mem(struct optee_msg_param *mp,memset (&mp->u,0 ,sizeof (mp->u)); mp->u.fmem.global_id =OPTEE_MSG_FMEM_INVALID_GLOBAL_ID ; } +out: mp->u.fmem.size = p->u.memref.size;return 0 ;@@ -222,13 +241,15 @@ static int to_msg_param_ffa_mem(struct optee_msg_param *mp,* @optee: main service struct * @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays -* @params: subsystem itnernal parameter representation +* @params: subsystem internal parameter representation +* @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_ffa_to_msg_param (struct optee *optee,struct optee_msg_param *msg_params,size_t num_params, -const struct tee_param *params) +const struct tee_param *params, +bool update_out) {size_t n;@@ -238,18 +259,20 @@ static int optee_ffa_to_msg_param(struct optee *optee,switch (p->attr) {case TEE_IOCTL_PARAM_ATTR_TYPE_NONE : +if (update_out) +break ; mp->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&mp->u,0 ,sizeof (mp->u));break ;case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT : -optee_to_msg_param_value (mp, p); +optee_to_msg_param_value (mp, p, update_out);break ;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 (to_msg_param_ffa_mem (mp, p)) +if (to_msg_param_ffa_mem (mp, p, update_out))return -EINVAL ;break ;default :drivers/tee/optee/optee_private.h +28
-14@@ -185,10 +185,12 @@ struct optee_ops { bool system_thread);int (*to_msg_param)(struct optee *optee,struct optee_msg_param *msg_params, -size_t num_params,const struct tee_param *params); +size_t num_params,const struct tee_param *params, +bool update_out);int (*from_msg_param)(struct optee *optee,struct tee_param *params,size_t num_params, -const struct optee_msg_param *msg_params); +const struct optee_msg_param *msg_params, +bool update_out); };/** @@ -316,23 +318,35 @@ void optee_release(struct tee_context *ctx);void optee_release_supp (struct tee_context *ctx);static inline void optee_from_msg_param_value (struct tee_param *p,u32 attr, -const struct optee_msg_param *mp) +const struct optee_msg_param *mp, +bool update_out) { - p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + - attr -OPTEE_MSG_ATTR_TYPE_VALUE_INPUT ; - p->u.value.a = mp->u.value.a; - p->u.value.b = mp->u.value.b; - p->u.value.c = mp->u.value.c; +if (!update_out) + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + + attr -OPTEE_MSG_ATTR_TYPE_VALUE_INPUT ; + +if (attr ==OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT || + attr ==OPTEE_MSG_ATTR_TYPE_VALUE_INOUT || !update_out) { + p->u.value.a = mp->u.value.a; + p->u.value.b = mp->u.value.b; + p->u.value.c = mp->u.value.c; + } }static inline void optee_to_msg_param_value (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p, +bool update_out) { - mp->attr =OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - -TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT ; - mp->u.value.a = p->u.value.a; - mp->u.value.b = p->u.value.b; - mp->u.value.c = p->u.value.c; +if (!update_out) + mp->attr =OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - +TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT ; + +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || + p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT || !update_out) { + mp->u.value.a = p->u.value.a; + mp->u.value.b = p->u.value.b; + mp->u.value.c = p->u.value.c; + } }void optee_cq_init (struct optee_call_queue *cq,int thread_count);drivers/tee/optee/rpc.c +21
-10@@ -63,7 +63,7 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, }if (optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params)) + arg->params, false/*!update_out*/ )) goto bad;for (i =0 ; i < arg->num_params; i++) {@@ -107,7 +107,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, }else { params[3 ].u.value.a = msg.len;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) + arg->num_params, params, + true/*update_out*/ )) arg->ret =TEEC_ERROR_BAD_PARAMETERS ;else arg->ret =TEEC_SUCCESS ;@@ -188,6 +189,7 @@ static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)static void handle_rpc_supp_cmd (struct tee_context *ctx,struct optee *optee,struct optee_msg_arg *arg) { +bool update_out = false;struct tee_param *params; arg->ret_origin =TEEC_ORIGIN_COMMS ;@@ -200,15 +202,21 @@ static void handle_rpc_supp_cmd(struct tee_context *ctx, struct optee *optee, }if (optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params)) { + arg->params, update_out)) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ; goto out; } arg->ret =optee_supp_thrd_req (ctx, arg->cmd, arg->num_params, params); +/* +* Special treatment for OPTEE_RPC_CMD_SHM_ALLOC since input is a +* value type, but the output is a memref type. +*/ +if (arg->cmd !=OPTEE_RPC_CMD_SHM_ALLOC ) + update_out = true;if (optee->ops->to_msg_param (optee, arg->params, arg->num_params, - params)) + params, update_out)) arg->ret =TEEC_ERROR_BAD_PARAMETERS ; out:kfree (params);@@ -270,7 +278,7 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ;@@ -280,7 +288,8 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee_context *ctx, params[0 ].u.value.b =0 ; params[0 ].u.value.c =0 ;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ; }@@ -324,7 +333,7 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || params[1 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;@@ -358,7 +367,8 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_context *ctx, params[0 ].u.value.b = rdev->descr.capacity; params[0 ].u.value.c = rdev->descr.reliable_wr_count;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ; }@@ -384,7 +394,7 @@ static void handle_rpc_func_rpmb_frames(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT || params[1 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;@@ -401,7 +411,8 @@ static void handle_rpc_func_rpmb_frames(struct tee_context *ctx, goto out; }if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ; goto out; }drivers/tee/optee/smc_abi.c +56
-20@@ -81,20 +81,26 @@ static int optee_cpuhp_disable_pcpu_irq(unsigned int cpu)*/ static int from_msg_param_tmp_mem (struct tee_param *p,u32 attr, -const struct optee_msg_param *mp) +const struct optee_msg_param *mp, +bool update_out) {struct tee_shm *shm;phys_addr_t pa;int rc; +if (update_out) { +if (attr ==OPTEE_MSG_ATTR_TYPE_TMEM_INPUT ) +return 0 ; + goto out; + } + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -OPTEE_MSG_ATTR_TYPE_TMEM_INPUT ; - p->u.memref.size = mp->u.tmem.size; shm = (struct tee_shm *)(unsigned long )mp->u.tmem.shm_ref;if (!shm) { p->u.memref.shm_offs =0 ; p->u.memref.shm =NULL ; -return 0 ; + goto out; } rc =tee_shm_get_pa (shm,0 , &pa);@@ -103,18 +109,25 @@ static int from_msg_param_tmp_mem(struct tee_param *p, u32 attr, p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa; p->u.memref.shm = shm; - +out: + p->u.memref.size = mp->u.tmem.size;return 0 ; }static void from_msg_param_reg_mem (struct tee_param *p,u32 attr, -const struct optee_msg_param *mp) +const struct optee_msg_param *mp, +bool update_out) {struct tee_shm *shm; +if (update_out) { +if (attr ==OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ) +return ; + goto out; + } + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ; - p->u.memref.size = mp->u.rmem.size; shm = (struct tee_shm *)(unsigned long )mp->u.rmem.shm_ref;if (shm) {@@ -124,6 +137,8 @@ static void from_msg_param_reg_mem(struct tee_param *p, u32 attr, p->u.memref.shm_offs =0 ; p->u.memref.shm =NULL ; } +out: + p->u.memref.size = mp->u.rmem.size; }/** @@ -133,11 +148,13 @@ static void from_msg_param_reg_mem(struct tee_param *p, u32 attr,* @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters +* @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_from_msg_param (struct optee *optee,struct tee_param *params,size_t num_params, -const struct optee_msg_param *msg_params) +const struct optee_msg_param *msg_params, +bool update_out) {int rc;size_t n;@@ -149,25 +166,27 @@ static int optee_from_msg_param(struct optee *optee, struct tee_param *params,switch (attr) {case OPTEE_MSG_ATTR_TYPE_NONE : +if (update_out) +break ; p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&p->u,0 ,sizeof (p->u));break ;case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT : -optee_from_msg_param_value (p, attr, mp); +optee_from_msg_param_value (p, attr, mp, update_out);break ;case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT :case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT :case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT : - rc =from_msg_param_tmp_mem (p, attr, mp); + rc =from_msg_param_tmp_mem (p, attr, mp, update_out);if (rc)return rc;break ;case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT :case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT :case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT : -from_msg_param_reg_mem (p, attr, mp); +from_msg_param_reg_mem (p, attr, mp, update_out);break ;default :@@ -178,20 +197,25 @@ static int optee_from_msg_param(struct optee *optee, struct tee_param *params, }static int to_msg_param_tmp_mem (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p,bool update_out) {int rc;phys_addr_t pa; +if (update_out) { +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ) +return 0 ; + goto out; + } + mp->attr =OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ; mp->u.tmem.shm_ref = (unsigned long )p->u.memref.shm; - mp->u.tmem.size = p->u.memref.size;if (!p->u.memref.shm) { mp->u.tmem.buf_ptr =0 ; -return 0 ; + goto out; } rc =tee_shm_get_pa (p->u.memref.shm, p->u.memref.shm_offs, &pa);@@ -201,19 +225,27 @@ static int to_msg_param_tmp_mem(struct optee_msg_param *mp, mp->u.tmem.buf_ptr = pa; mp->attr |=OPTEE_MSG_ATTR_CACHE_PREDEFINED <<OPTEE_MSG_ATTR_CACHE_SHIFT ; - +out: + mp->u.tmem.size = p->u.memref.size;return 0 ; }static int to_msg_param_reg_mem (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p,bool update_out) { +if (update_out) { +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ) +return 0 ; + goto out; + } + mp->attr =OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ; mp->u.rmem.shm_ref = (unsigned long )p->u.memref.shm; - mp->u.rmem.size = p->u.memref.size; mp->u.rmem.offs = p->u.memref.shm_offs; +out: + mp->u.rmem.size = p->u.memref.size;return 0 ; }@@ -223,11 +255,13 @@ static int to_msg_param_reg_mem(struct optee_msg_param *mp,* @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays * @params: subsystem itnernal parameter representation +* @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_to_msg_param (struct optee *optee,struct optee_msg_param *msg_params, -size_t num_params,const struct tee_param *params) +size_t num_params,const struct tee_param *params, +bool update_out) {int rc;size_t n;@@ -238,21 +272,23 @@ static int optee_to_msg_param(struct optee *optee,switch (p->attr) {case TEE_IOCTL_PARAM_ATTR_TYPE_NONE : +if (update_out) +break ; mp->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&mp->u,0 ,sizeof (mp->u));break ;case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT : -optee_to_msg_param_value (mp, p); +optee_to_msg_param_value (mp, p, update_out);break ;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_dynamic (p->u.memref.shm)) - rc =to_msg_param_reg_mem (mp, p); + rc =to_msg_param_reg_mem (mp, p, update_out);else - rc =to_msg_param_tmp_mem (mp, p); + rc =to_msg_param_tmp_mem (mp, p, update_out);if (rc)return rc;break ;Signature · 1 line
2.43.0
Re: [PATCH v8 03/14] optee: account for direction while converting parameters
Hi Sumit,
Quoted 31 lines · Sumit Garg
Hi Jens,
Quoted 17 lines · Jens Wiklander
The OP-TEE backend driver has two internal function pointers to convert between the subsystem type struct
tee_paramand the OP-TEE type structoptee_msg_param.The conversion is done from one of the types to the other, which is then involved in some operation and finally converted back to the original type. When converting to prepare the parameters for the operation, all fields must be taken into account, but then converting back, it's enough to update only out-values and out-sizes. So, an
update_outparameter is added to the conversion functions to tell if all or only some fields must be copied.This is needed in a later patch where it might get confusing when converting back in
from_msg_param()callback since an allocated restricted SHM can be using thesec_world_idof the used restricted memory pool and that doesn't translate back well.Quoted 9 lines
Signed-off-by Jens Wiklander <[email protected]>--- drivers/tee/optee/call.c | 10 ++-- drivers/tee/optee/ffa_abi.c | 43 +++++++++++++---- drivers/tee/optee/optee_private.h | 42 +++++++++++------ drivers/tee/optee/rpc.c | 31 +++++++++---- drivers/tee/optee/smc_abi.c | 76 +++++++++++++++++++++++-------- 5 files changed, 144 insertions(+), 58 deletions(-)
Quoted 4 lines
Thinking about it a bit more, as you mentioned in v6 that this patch isn't strictly required for this series, can we drop this from this series and rather followup with a separate patch? If you agree then I can give a try on simplifying this patch?
Sure. Who knows, perhaps it will be ready before a version of the patch is accepted?
Cheers,
Jens
Quoted 590 lines
-Sumit
Quoted 587 lines
drivers/tee/optee/call.c +6
-4@@ -400,7 +400,8 @@ int optee_open_session(struct tee_context *ctx,export_uuid (msg_arg->params[1 ].u.octets, &client_uuid); rc = optee->ops->to_msg_param (optee, msg_arg->params +2 , - arg->num_params, param); + arg->num_params, param, + false/*!update_out*/ );if (rc) goto out;@@ -427,7 +428,8 @@ int optee_open_session(struct tee_context *ctx, }if (optee->ops->from_msg_param (optee, param, arg->num_params, - msg_arg->params +2 )) { + msg_arg->params +2 , + true/*update_out*/ )) { arg->ret =TEEC_ERROR_COMMUNICATION ; arg->ret_origin =TEEC_ORIGIN_COMMS ;/* Close session again to avoid leakage */ @@ -541,7 +543,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, msg_arg->cancel_id = arg->cancel_id; rc = optee->ops->to_msg_param (optee, msg_arg->params, arg->num_params, - param); + param, false/*!update_out*/ );if (rc) goto out;@@ -551,7 +553,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, }if (optee->ops->from_msg_param (optee, param, arg->num_params, - msg_arg->params)) { + msg_arg->params, true/*update_out*/ )) { msg_arg->ret =TEEC_ERROR_COMMUNICATION ; msg_arg->ret_origin =TEEC_ORIGIN_COMMS ; }drivers/tee/optee/ffa_abi.c +33
-10@@ -122,15 +122,21 @@ static int optee_shm_rem_ffa_handle(struct optee *optee, u64 global_id)*/ static void from_msg_param_ffa_mem (struct optee *optee,struct tee_param *p, -u32 attr,const struct optee_msg_param *mp) +u32 attr,const struct optee_msg_param *mp, +bool update_out) {struct tee_shm *shm =NULL ;u64 offs_high =0 ;u64 offs_low =0 ; +if (update_out) { +if (attr ==OPTEE_MSG_ATTR_TYPE_FMEM_INPUT ) +return ; + goto out; + } + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -OPTEE_MSG_ATTR_TYPE_FMEM_INPUT ; - p->u.memref.size = mp->u.fmem.size;if (mp->u.fmem.global_id !=OPTEE_MSG_FMEM_INVALID_GLOBAL_ID ) shm =optee_shm_from_ffa_handle (optee, mp->u.fmem.global_id);@@ -141,6 +147,8 @@ static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p, offs_high = mp->u.fmem.offs_high; } p->u.memref.shm_offs = offs_low | offs_high <<32 ; +out: + p->u.memref.size = mp->u.fmem.size; }/** @@ -150,12 +158,14 @@ static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p,* @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters +* @update_out: update parameter for output only * * Returns 0 on success or <0 on failure */ static int optee_ffa_from_msg_param (struct optee *optee,struct tee_param *params,size_t num_params, -const struct optee_msg_param *msg_params) +const struct optee_msg_param *msg_params, +bool update_out) {size_t n;@@ -166,18 +176,20 @@ static int optee_ffa_from_msg_param(struct optee *optee,switch (attr) {case OPTEE_MSG_ATTR_TYPE_NONE : +if (update_out) +break ; p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&p->u,0 ,sizeof (p->u));break ;case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT : -optee_from_msg_param_value (p, attr, mp); +optee_from_msg_param_value (p, attr, mp, update_out);break ;case OPTEE_MSG_ATTR_TYPE_FMEM_INPUT :case OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT :case OPTEE_MSG_ATTR_TYPE_FMEM_INOUT : -from_msg_param_ffa_mem (optee, p, attr, mp); +from_msg_param_ffa_mem (optee, p, attr, mp, update_out);break ;default :return -EINVAL ;@@ -188,10 +200,16 @@ static int optee_ffa_from_msg_param(struct optee *optee, }static int to_msg_param_ffa_mem (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p,bool update_out) {struct tee_shm *shm = p->u.memref.shm; +if (update_out) { +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ) +return 0 ; + goto out; + } + mp->attr =OPTEE_MSG_ATTR_TYPE_FMEM_INPUT + p->attr -TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ;@@ -211,6 +229,7 @@ static int to_msg_param_ffa_mem(struct optee_msg_param *mp,memset (&mp->u,0 ,sizeof (mp->u)); mp->u.fmem.global_id =OPTEE_MSG_FMEM_INVALID_GLOBAL_ID ; } +out: mp->u.fmem.size = p->u.memref.size;return 0 ;@@ -222,13 +241,15 @@ static int to_msg_param_ffa_mem(struct optee_msg_param *mp,* @optee: main service struct * @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays -* @params: subsystem itnernal parameter representation +* @params: subsystem internal parameter representation +* @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_ffa_to_msg_param (struct optee *optee,struct optee_msg_param *msg_params,size_t num_params, -const struct tee_param *params) +const struct tee_param *params, +bool update_out) {size_t n;@@ -238,18 +259,20 @@ static int optee_ffa_to_msg_param(struct optee *optee,switch (p->attr) {case TEE_IOCTL_PARAM_ATTR_TYPE_NONE : +if (update_out) +break ; mp->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&mp->u,0 ,sizeof (mp->u));break ;case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT : -optee_to_msg_param_value (mp, p); +optee_to_msg_param_value (mp, p, update_out);break ;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 (to_msg_param_ffa_mem (mp, p)) +if (to_msg_param_ffa_mem (mp, p, update_out))return -EINVAL ;break ;default :drivers/tee/optee/optee_private.h +28
-14@@ -185,10 +185,12 @@ struct optee_ops { bool system_thread);int (*to_msg_param)(struct optee *optee,struct optee_msg_param *msg_params, -size_t num_params,const struct tee_param *params); +size_t num_params,const struct tee_param *params, +bool update_out);int (*from_msg_param)(struct optee *optee,struct tee_param *params,size_t num_params, -const struct optee_msg_param *msg_params); +const struct optee_msg_param *msg_params, +bool update_out); };/** @@ -316,23 +318,35 @@ void optee_release(struct tee_context *ctx);void optee_release_supp (struct tee_context *ctx);static inline void optee_from_msg_param_value (struct tee_param *p,u32 attr, -const struct optee_msg_param *mp) +const struct optee_msg_param *mp, +bool update_out) { - p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + - attr -OPTEE_MSG_ATTR_TYPE_VALUE_INPUT ; - p->u.value.a = mp->u.value.a; - p->u.value.b = mp->u.value.b; - p->u.value.c = mp->u.value.c; +if (!update_out) + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + + attr -OPTEE_MSG_ATTR_TYPE_VALUE_INPUT ; + +if (attr ==OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT || + attr ==OPTEE_MSG_ATTR_TYPE_VALUE_INOUT || !update_out) { + p->u.value.a = mp->u.value.a; + p->u.value.b = mp->u.value.b; + p->u.value.c = mp->u.value.c; + } }static inline void optee_to_msg_param_value (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p, +bool update_out) { - mp->attr =OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - -TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT ; - mp->u.value.a = p->u.value.a; - mp->u.value.b = p->u.value.b; - mp->u.value.c = p->u.value.c; +if (!update_out) + mp->attr =OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - +TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT ; + +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || + p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT || !update_out) { + mp->u.value.a = p->u.value.a; + mp->u.value.b = p->u.value.b; + mp->u.value.c = p->u.value.c; + } }void optee_cq_init (struct optee_call_queue *cq,int thread_count);drivers/tee/optee/rpc.c +21
-10@@ -63,7 +63,7 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, }if (optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params)) + arg->params, false/*!update_out*/ )) goto bad;for (i =0 ; i < arg->num_params; i++) {@@ -107,7 +107,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, }else { params[3 ].u.value.a = msg.len;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) + arg->num_params, params, + true/*update_out*/ )) arg->ret =TEEC_ERROR_BAD_PARAMETERS ;else arg->ret =TEEC_SUCCESS ;@@ -188,6 +189,7 @@ static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)static void handle_rpc_supp_cmd (struct tee_context *ctx,struct optee *optee,struct optee_msg_arg *arg) { +bool update_out = false;struct tee_param *params; arg->ret_origin =TEEC_ORIGIN_COMMS ;@@ -200,15 +202,21 @@ static void handle_rpc_supp_cmd(struct tee_context *ctx, struct optee *optee, }if (optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params)) { + arg->params, update_out)) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ; goto out; } arg->ret =optee_supp_thrd_req (ctx, arg->cmd, arg->num_params, params); +/* +* Special treatment for OPTEE_RPC_CMD_SHM_ALLOC since input is a +* value type, but the output is a memref type. +*/ +if (arg->cmd !=OPTEE_RPC_CMD_SHM_ALLOC ) + update_out = true;if (optee->ops->to_msg_param (optee, arg->params, arg->num_params, - params)) + params, update_out)) arg->ret =TEEC_ERROR_BAD_PARAMETERS ; out:kfree (params);@@ -270,7 +278,7 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ;@@ -280,7 +288,8 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee_context *ctx, params[0 ].u.value.b =0 ; params[0 ].u.value.c =0 ;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ; }@@ -324,7 +333,7 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || params[1 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;@@ -358,7 +367,8 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_context *ctx, params[0 ].u.value.b = rdev->descr.capacity; params[0 ].u.value.c = rdev->descr.reliable_wr_count;if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;return ; }@@ -384,7 +394,7 @@ static void handle_rpc_func_rpmb_frames(struct tee_context *ctx,if (arg->num_params !=ARRAY_SIZE (params) || optee->ops->from_msg_param (optee, params, arg->num_params, - arg->params) || + arg->params, false/*!update_out*/ ) || params[0 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT || params[1 ].attr !=TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ;@@ -401,7 +411,8 @@ static void handle_rpc_func_rpmb_frames(struct tee_context *ctx, goto out; }if (optee->ops->to_msg_param (optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true/*update_out*/ )) { arg->ret =TEEC_ERROR_BAD_PARAMETERS ; goto out; }drivers/tee/optee/smc_abi.c +56
-21@@ -81,20 +81,26 @@ static int optee_cpuhp_disable_pcpu_irq(unsigned int cpu)*/ static int from_msg_param_tmp_mem (struct tee_param *p,u32 attr, -const struct optee_msg_param *mp) +const struct optee_msg_param *mp, +bool update_out) {struct tee_shm *shm;phys_addr_t pa;int rc; +if (update_out) { +if (attr ==OPTEE_MSG_ATTR_TYPE_TMEM_INPUT ) +return 0 ; + goto out; + } + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -OPTEE_MSG_ATTR_TYPE_TMEM_INPUT ; - p->u.memref.size = mp->u.tmem.size; shm = (struct tee_shm *)(unsigned long )mp->u.tmem.shm_ref;if (!shm) { p->u.memref.shm_offs =0 ; p->u.memref.shm =NULL ; -return 0 ; + goto out; } rc =tee_shm_get_pa (shm,0 , &pa);@@ -103,18 +109,25 @@ static int from_msg_param_tmp_mem(struct tee_param *p, u32 attr, p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa; p->u.memref.shm = shm; - +out: + p->u.memref.size = mp->u.tmem.size;return 0 ; }static void from_msg_param_reg_mem (struct tee_param *p,u32 attr, -const struct optee_msg_param *mp) +const struct optee_msg_param *mp, +bool update_out) {struct tee_shm *shm; +if (update_out) { +if (attr ==OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ) +return ; + goto out; + } + p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ; - p->u.memref.size = mp->u.rmem.size; shm = (struct tee_shm *)(unsigned long )mp->u.rmem.shm_ref;if (shm) {@@ -124,6 +137,8 @@ static void from_msg_param_reg_mem(struct tee_param *p, u32 attr, p->u.memref.shm_offs =0 ; p->u.memref.shm =NULL ; } +out: + p->u.memref.size = mp->u.rmem.size; }/** @@ -133,11 +148,13 @@ static void from_msg_param_reg_mem(struct tee_param *p, u32 attr,* @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters +* @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_from_msg_param (struct optee *optee,struct tee_param *params,size_t num_params, -const struct optee_msg_param *msg_params) +const struct optee_msg_param *msg_params, +bool update_out) {int rc;size_t n;@@ -149,25 +166,27 @@ static int optee_from_msg_param(struct optee *optee, struct tee_param *params,switch (attr) {case OPTEE_MSG_ATTR_TYPE_NONE : +if (update_out) +break ; p->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&p->u,0 ,sizeof (p->u));break ;case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT :case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT : -optee_from_msg_param_value (p, attr, mp); +optee_from_msg_param_value (p, attr, mp, update_out);break ;case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT :case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT :case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT : - rc =from_msg_param_tmp_mem (p, attr, mp); + rc =from_msg_param_tmp_mem (p, attr, mp, update_out);if (rc)return rc;break ;case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT :case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT :case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT : -from_msg_param_reg_mem (p, attr, mp); +from_msg_param_reg_mem (p, attr, mp, update_out);break ;default :@@ -178,20 +197,25 @@ static int optee_from_msg_param(struct optee *optee, struct tee_param *params, }static int to_msg_param_tmp_mem (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p,bool update_out) {int rc;phys_addr_t pa; +if (update_out) { +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ) +return 0 ; + goto out; + } + mp->attr =OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ; mp->u.tmem.shm_ref = (unsigned long )p->u.memref.shm; - mp->u.tmem.size = p->u.memref.size;if (!p->u.memref.shm) { mp->u.tmem.buf_ptr =0 ; -return 0 ; + goto out; } rc =tee_shm_get_pa (p->u.memref.shm, p->u.memref.shm_offs, &pa);@@ -201,19 +225,27 @@ static int to_msg_param_tmp_mem(struct optee_msg_param *mp, mp->u.tmem.buf_ptr = pa; mp->attr |=OPTEE_MSG_ATTR_CACHE_PREDEFINED <<OPTEE_MSG_ATTR_CACHE_SHIFT ; - +out: + mp->u.tmem.size = p->u.memref.size;return 0 ; }static int to_msg_param_reg_mem (struct optee_msg_param *mp, -const struct tee_param *p) +const struct tee_param *p,bool update_out) { +if (update_out) { +if (p->attr ==TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ) +return 0 ; + goto out; + } + mp->attr =OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT ; mp->u.rmem.shm_ref = (unsigned long )p->u.memref.shm; - mp->u.rmem.size = p->u.memref.size; mp->u.rmem.offs = p->u.memref.shm_offs; +out: + mp->u.rmem.size = p->u.memref.size;return 0 ; }@@ -223,11 +255,13 @@ static int to_msg_param_reg_mem(struct optee_msg_param *mp,* @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays * @params: subsystem itnernal parameter representation +* @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_to_msg_param (struct optee *optee,struct optee_msg_param *msg_params, -size_t num_params,const struct tee_param *params) +size_t num_params,const struct tee_param *params, +bool update_out) {int rc;size_t n;@@ -238,21 +272,23 @@ static int optee_to_msg_param(struct optee *optee,switch (p->attr) {case TEE_IOCTL_PARAM_ATTR_TYPE_NONE : +if (update_out) +break ; mp->attr =TEE_IOCTL_PARAM_ATTR_TYPE_NONE ;memset (&mp->u,0 ,sizeof (mp->u));break ;case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT :case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT : -optee_to_msg_param_value (mp, p); +optee_to_msg_param_value (mp, p, update_out);break ;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_dynamic (p->u.memref.shm)) - rc =to_msg_param_reg_mem (mp, p); + rc =to_msg_param_reg_mem (mp, p, update_out);else - rc =to_msg_param_tmp_mem (mp, p); + rc =to_msg_param_tmp_mem (mp, p, update_out);if (rc)return rc;break ; --2.43.0
PATCH v8 4/14 optee: sync secure world ABI headers
In reply to Jens WiklanderUpdate the header files describing the secure world ABI, both with and without FF-A. The ABI is extended to deal with protected memory, but as usual backward compatible.
--- drivers/tee/optee/optee_ffa.h | 27 +++++++++--- drivers/tee/optee/optee_msg.h | 83 ++++++++++++++++++++++++++++++----- drivers/tee/optee/optee_smc.h | 69 ++++++++++++++++++++++++++++- 3 files changed, 161 insertions(+), 18 deletions(-)
drivers/tee/optee/optee_ffa.h +22 -5
@@ -81,7 +81,7 @@* as the second MSG arg struct for * OPTEE_FFA_YIELDING_CALL_WITH_ARG. * Bit[31:8]: Reserved (MBZ) -* w5: Bitfield of secure world capabilities OPTEE_FFA_SEC_CAP_* below, +* w5: Bitfield of OP-TEE capabilities OPTEE_FFA_SEC_CAP_* * w6: The maximum secure world notification number * w7: Not used (MBZ) */
@@ -94,6 +94,8 @@#define OPTEE_FFA_SEC_CAP_ASYNC_NOTIF BIT(1)/* OP-TEE supports probing for RPMB device if needed */ #define OPTEE_FFA_SEC_CAP_RPMB_PROBE BIT(2) +/* OP-TEE supports Protected Memory for secure data path */ +#define OPTEE_FFA_SEC_CAP_PROTMEM BIT(3)#define OPTEE_FFA_EXCHANGE_CAPABILITIES OPTEE_FFA_BLOCKING_CALL(2)
@@ -108,7 +110,7 @@* * Return register usage: * w3: Error code, 0 on success -* w4-w7: Note used (MBZ) +* w4-w7: Not used (MBZ) */ #define OPTEE_FFA_UNREGISTER_SHM OPTEE_FFA_BLOCKING_CALL(3)
@@ -119,16 +121,31 @@* Call register usage: * w3: Service ID, OPTEE_FFA_ENABLE_ASYNC_NOTIF * w4: Notification value to request bottom half processing, should be -* less than OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE. +* less than OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE * w5-w7: Not used (MBZ) * * Return register usage: * w3: Error code, 0 on success -* w4-w7: Note used (MBZ) +* w4-w7: Not used (MBZ) */ #define OPTEE_FFA_ENABLE_ASYNC_NOTIF OPTEE_FFA_BLOCKING_CALL(5) -#define OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE 64 +#define OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE 64 + +/* +* Release Protected memory +* +* Call register usage: +* w3: Service ID, OPTEE_FFA_RECLAIM_PROTMEM +* w4: Shared memory handle, lower bits +* w5: Shared memory handle, higher bits +* w6-w7: Not used (MBZ) +* +* Return register usage: +* w3: Error code, 0 on success +* w4-w7: Note used (MBZ) +*/ +#define OPTEE_FFA_RELEASE_PROTMEM OPTEE_FFA_BLOCKING_CALL(8)/* * Call with struct optee_msg_arg as argument in the supplied shared memory
drivers/tee/optee/optee_msg.h +71 -12
112 lines not on this page · raw message
drivers/tee/optee/optee_smc.h +68 -1
87 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 5/14 dma-buf: dma-heap: export declared functions
In reply to Jens WiklanderExport the dma-buf heap functions declared in <linux/dma-heap.h>.
--- drivers/dma-buf/dma-heap.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/dma-buf/dma-heap.c +3 -0
@@ -202,6 +202,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) {return heap->priv; } +EXPORT_SYMBOL (dma_heap_get_drvdata);/** * dma_heap_get_name - get heap name
@@ -214,6 +215,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) {return heap->name; } +EXPORT_SYMBOL (dma_heap_get_name);/** * dma_heap_add - adds a heap to dmabuf heaps
@@ -303,6 +305,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)kfree (heap);return err_ret; } +EXPORT_SYMBOL (dma_heap_add);static char *dma_heap_devnode (const struct device *dev,umode_t *mode) {
Signature · 1 line
2.43.0
PATCH v8 6/14 tee: implement protected DMA-heap
In reply to Jens WiklanderImplement DMA heap for protected DMA-buf allocation in the TEE subsystem.
Restricted memory refers to memory buffers behind a hardware enforced firewall. It is not accessible to the kernel during normal circumstances but rather only accessible to certain hardware IPs or CPUs executing in higher or differently privileged mode than the kernel itself. This interface allows to allocate and manage such protected memory buffers via interaction with a TEE implementation.
The protected memory is allocated for a specific use-case, like Secure Video Playback, Trusted UI, or Secure Video Recording where certain hardware devices can access the memory.
The DMA-heaps are enabled explicitly by the TEE backend driver. The TEE backend drivers needs to implement protected memory pool to manage the protected memory.
--- drivers/tee/Makefile | 1 + drivers/tee/tee_heap.c | 470 ++++++++++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 6 + include/linux/tee_core.h | 65 ++++++ 4 files changed, 542 insertions(+) create mode 100644 drivers/tee/tee_heap.c
drivers/tee/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_TEE) += tee.o tee-objs += tee_core.o +tee-objs += tee_heap.o tee-objs += tee_shm.o tee-objs += tee_shm_pool.o obj-$(CONFIG_OPTEE) += optee/
drivers/tee/tee_heap.c new file +470 -0
470 lines not on this page · raw message
drivers/tee/tee_private.h +6 -0
@@ -8,6 +8,7 @@#include <linux/cdev.h> #include <linux/completion.h> #include <linux/device.h> +#include <linux/dma-buf.h> #include <linux/kref.h> #include <linux/mutex.h> #include <linux/types.h>
@@ -24,4 +25,9 @@ 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); +int tee_heap_update_from_dma_buf (struct tee_device *teedev, +struct dma_buf *dmabuf,size_t *offset, +struct tee_shm *shm, +struct tee_shm **parent_shm); +#endif /*TEE_PRIVATE_H*/
include/linux/tee_core.h +65 -0
98 lines not on this page · raw message
Signature · 1 line
2.43.0
Robin Murphy
reviewerRe: [PATCH v8 06/14] tee: implement protected DMA-heap
Quoted 10 lines · Jens Wiklander
Implement DMA heap for protected DMA-buf allocation in the TEE subsystem.
Restricted memory refers to memory buffers behind a hardware enforced firewall. It is not accessible to the kernel during normal circumstances but rather only accessible to certain hardware IPs or CPUs executing in higher or differently privileged mode than the kernel itself. This interface allows to allocate and manage such protected memory buffers via interaction with a TEE implementation.
Quoted 7 lines
The protected memory is allocated for a specific use-case, like Secure Video Playback, Trusted UI, or Secure Video Recording where certain hardware devices can access the memory.
The DMA-heaps are enabled explicitly by the TEE backend driver. The TEE backend drivers needs to implement protected memory pool to manage the protected memory.
[...]> +static struct sg_table *
drivers/tee/tee_heap.c line 109
Quoted 8 lines
drivers/tee/tee_heap.c new file +8
-0+tee_heap_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) +{ + struct tee_heap_attachment *a = attachment->priv; +int ret; + + ret =dma_map_sgtable (attachment->dev, &a->table, direction, +DMA_ATTR_SKIP_CPU_SYNC );
If the memory is inaccessible to the kernel, what does this DMA mapping even mean? What happens when it tries to perform cache maintenance or bounce-buffering on inaccessible memory (which presumably doesn't even have a VA if it's not usable as normal kernel memory)?
If we're simply housekeeping the TEE's resources on its behalf, and giving it back some token to tell it which resource to go do its thing with, then that's really not "DMA" as far as the kernel is concerned.
[...]
Quoted 9 lines
drivers/tee/tee_heap.c new file +9
-0+static int protmem_pool_op_static_alloc (struct tee_protmem_pool *pool, +struct sg_table *sgt,size_t size, +size_t *offs) +{ +struct tee_protmem_static_pool *stp =to_protmem_static_pool (pool); +phys_addr_t pa; +int ret; + + pa =gen_pool_alloc (stp->gen_pool, size);
drivers/tee/tee_heap.c line 388
Quoted 10 lines
drivers/tee/tee_heap.c new file +10
-0+if (!pa) +return -ENOMEM ; + + ret =sg_alloc_table (sgt,1 ,GFP_KERNEL ); +if (ret) { +gen_pool_free (stp->gen_pool, pa, size); +return ret; + } + +sg_set_page (sgt->sgl,phys_to_page (pa), size,0 );
Where does "pa" come from here (i.e. what's the provenance of the initial "paddr" passed to tee_protmem_static_pool_alloc())? In general we can't call {phys,pfn}_to_page() an arbitrary addresses without checking pfn_valid() first. A bogus address might even crash __pfn_to_page() itself under CONFIG_SPARSEMEM.
Thanks,
Robin.
Quoted 4 lines
drivers/tee/tee_heap.c new file +4
-0+ *offs = pa - stp->pa_base; + +return 0 ; +}
Re: [PATCH v8 06/14] tee: implement protected DMA-heap
Hi,
Quoted 21 lines · Robin Murphy
Quoted 10 lines · Jens Wiklander
Implement DMA heap for protected DMA-buf allocation in the TEE subsystem.
Restricted memory refers to memory buffers behind a hardware enforced firewall. It is not accessible to the kernel during normal circumstances but rather only accessible to certain hardware IPs or CPUs executing in higher or differently privileged mode than the kernel itself. This interface allows to allocate and manage such protected memory buffers via interaction with a TEE implementation.
Quoted 7 lines
The protected memory is allocated for a specific use-case, like Secure Video Playback, Trusted UI, or Secure Video Recording where certain hardware devices can access the memory.
The DMA-heaps are enabled explicitly by the TEE backend driver. The TEE backend drivers needs to implement protected memory pool to manage the protected memory.
[...]> +static struct
sg_table*
Quoted 13 lines
drivers/tee/tee_heap.c line 109
Quoted 8 lines
drivers/tee/tee_heap.c new file +8
-0+tee_heap_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) +{ + struct tee_heap_attachment *a = attachment->priv; +int ret; + + ret =dma_map_sgtable (attachment->dev, &a->table, direction, +DMA_ATTR_SKIP_CPU_SYNC );If the memory is inaccessible to the kernel, what does this DMA mapping even mean? What happens when it tries to perform cache maintenance or bounce-buffering on inaccessible memory (which presumably doesn't even have a VA if it's not usable as normal kernel memory)?
Doesn't DMA_ATTR_SKIP_CPU_SYNC say that the kernel shouldn't perform cache maintenance on the buffer since it's already in the device domain? The device is expected to be permitted to access the memory.
Quoted 4 lines
If we're simply housekeeping the TEE's resources on its behalf, and giving it back some token to tell it which resource to go do its thing with, then that's really not "DMA" as far as the kernel is concerned.
These buffers are supposed to be passed to devices that might be under only partial control of the kernel.
Quoted 19 lines
[...]
Quoted 17 lines
drivers/tee/tee_heap.c new file +17
-0+static int protmem_pool_op_static_alloc (struct tee_protmem_pool *pool, +struct sg_table *sgt,size_t size, +size_t *offs) +{ +struct tee_protmem_static_pool *stp =to_protmem_static_pool (pool); +phys_addr_t pa; +int ret; + + pa =gen_pool_alloc (stp->gen_pool, size); +if (!pa) +return -ENOMEM ; + + ret =sg_alloc_table (sgt,1 ,GFP_KERNEL ); +if (ret) { +gen_pool_free (stp->gen_pool, pa, size); +return ret; + }
Quoted 8 lines
drivers/tee/tee_heap.c line 388
Quoted 2 lines
drivers/tee/tee_heap.c new file +2
-0+ +sg_set_page (sgt->sgl,phys_to_page (pa), size,0 );Where does "pa" come from here (i.e. what's the provenance of the initial "paddr" passed to
tee_protmem_static_pool_alloc())? In general we can't call {phys,pfn}_to_page() an arbitrary addresses without checkingpfn_valid()first. A bogus address might even crash__pfn_to_page()itself underCONFIG_SPARSEMEM.
That's a good point. Would it be enough to check the address with pfn_valid() in tee_protmem_static_pool_alloc()?
I expect that the memory is normally carved out of DDR and made secure or protected in a platform-specific way, either at boot with a static carveout or dynamically after boot.
Thanks,
Jens
Quoted 8 lines
Thanks,
Robin.Quoted 4 lines
drivers/tee/tee_heap.c new file +4
-0+ *offs = pa - stp->pa_base; + +return 0 ; +}
PATCH v8 7/14 tee: refactor params_from_user()
In reply to Jens WiklanderBreak out the memref handling into a separate helper function. No change in behavior.
--- drivers/tee/tee_core.c | 94 ++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 40 deletions(-)
drivers/tee/tee_core.c +54 -40
@@ -353,6 +353,55 @@ tee_ioctl_shm_register(struct tee_context *ctx,return ret; } +static int param_from_user_memref (struct tee_context *ctx, +struct tee_param_memref *memref, +struct tee_ioctl_param *ip) +{ +struct tee_shm *shm; + +/* +* If a NULL pointer is passed to a TA in the TEE, +* the ip.c IOCTL parameters is set to TEE_MEMREF_NULL +* indicating a NULL memory reference. +*/ +if (ip->c !=TEE_MEMREF_NULL ) { +/* +* If we fail to get a pointer to a shared +* memory object (and increase the ref count) +* from an identifier we return an error. All +* pointers that has been added in params have +* an increased ref count. It's the callers +* responibility to do tee_shm_put() on all +* resolved pointers. +*/ + shm =tee_shm_get_from_id (ctx, ip->c); +if (IS_ERR (shm)) +return PTR_ERR (shm); + +/* +* Ensure offset + size does not overflow +* offset and does not overflow the size of +* the referred shared memory object. +*/ +if ((ip->a + ip->b) < ip->a || + (ip->a + ip->b) > shm->size) { +tee_shm_put (shm); +return -EINVAL ; + } + }else if (ctx->cap_memref_null) { +/* Pass NULL pointer to OP-TEE */ + shm =NULL ; + }else { +return -EINVAL ; + } + + memref->shm_offs = ip->a; + memref->size = ip->b; + memref->shm = shm; + +return 0 ; +} +static int params_from_user (struct tee_context *ctx,struct tee_param *params,size_t num_params,struct tee_ioctl_param __user *uparams)
@@ -360,8 +409,8 @@ static int params_from_user(struct tee_context *ctx, struct tee_param *params,size_t n;for (n =0 ; n < num_params; n++) { -struct tee_shm *shm;struct tee_ioctl_param ip; +int rc;if (copy_from_user (&ip, uparams + n,sizeof (ip)))return -EFAULT ;
@@ -384,45 +433,10 @@ static int params_from_user(struct tee_context *ctx, struct tee_param *params,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 a NULL pointer is passed to a TA in the TEE, -* the ip.c IOCTL parameters is set to TEE_MEMREF_NULL -* indicating a NULL memory reference. -*/ -if (ip.c !=TEE_MEMREF_NULL ) { -/* -* If we fail to get a pointer to a shared -* memory object (and increase the ref count) -* from an identifier we return an error. All -* pointers that has been added in params have -* an increased ref count. It's the callers -* responibility to do tee_shm_put() on all -* resolved pointers. -*/ - shm =tee_shm_get_from_id (ctx, ip.c); -if (IS_ERR (shm)) -return PTR_ERR (shm); - -/* -* Ensure offset + size does not overflow -* offset and does not overflow the size of -* the referred shared memory object. -*/ -if ((ip.a + ip.b) < ip.a || - (ip.a + ip.b) > shm->size) { -tee_shm_put (shm); -return -EINVAL ; - } - }else if (ctx->cap_memref_null) { -/* Pass NULL pointer to OP-TEE */ - shm =NULL ; - }else { -return -EINVAL ; - } - - params[n].u.memref.shm_offs = ip.a; - params[n].u.memref.size = ip.b; - params[n].u.memref.shm = shm; + rc =param_from_user_memref (ctx, ¶ms[n].u.memref, + &ip); +if (rc) +return rc;break ;default :/* Unknown attribute */
Signature · 1 line
2.43.0
Re: [PATCH v8 07/14] tee: refactor params_from_user()
Quoted 7 lines · Jens Wiklander
Break out the memref handling into a separate helper function. No change in behavior.
Signed-off-by Jens Wiklander <[email protected]>--- drivers/tee/tee_core.c | 94 ++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 40 deletions(-)
-Sumit
Quoted 124 lines
drivers/tee/tee_core.c +54
-40@@ -353,6 +353,55 @@ tee_ioctl_shm_register(struct tee_context *ctx,return ret; } +static int param_from_user_memref (struct tee_context *ctx, +struct tee_param_memref *memref, +struct tee_ioctl_param *ip) +{ +struct tee_shm *shm; + +/* +* If a NULL pointer is passed to a TA in the TEE, +* the ip.c IOCTL parameters is set to TEE_MEMREF_NULL +* indicating a NULL memory reference. +*/ +if (ip->c !=TEE_MEMREF_NULL ) { +/* +* If we fail to get a pointer to a shared +* memory object (and increase the ref count) +* from an identifier we return an error. All +* pointers that has been added in params have +* an increased ref count. It's the callers +* responibility to do tee_shm_put() on all +* resolved pointers. +*/ + shm =tee_shm_get_from_id (ctx, ip->c); +if (IS_ERR (shm)) +return PTR_ERR (shm); + +/* +* Ensure offset + size does not overflow +* offset and does not overflow the size of +* the referred shared memory object. +*/ +if ((ip->a + ip->b) < ip->a || + (ip->a + ip->b) > shm->size) { +tee_shm_put (shm); +return -EINVAL ; + } + }else if (ctx->cap_memref_null) { +/* Pass NULL pointer to OP-TEE */ + shm =NULL ; + }else { +return -EINVAL ; + } + + memref->shm_offs = ip->a; + memref->size = ip->b; + memref->shm = shm; + +return 0 ; +} +static int params_from_user (struct tee_context *ctx,struct tee_param *params,size_t num_params,struct tee_ioctl_param __user *uparams)@@ -360,8 +409,8 @@ static int params_from_user(struct tee_context *ctx, struct tee_param *params,size_t n;for (n =0 ; n < num_params; n++) { -struct tee_shm *shm;struct tee_ioctl_param ip; +int rc;if (copy_from_user (&ip, uparams + n,sizeof (ip)))return -EFAULT ;@@ -384,45 +433,10 @@ static int params_from_user(struct tee_context *ctx, struct tee_param *params,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 a NULL pointer is passed to a TA in the TEE, -* the ip.c IOCTL parameters is set to TEE_MEMREF_NULL -* indicating a NULL memory reference. -*/ -if (ip.c !=TEE_MEMREF_NULL ) { -/* -* If we fail to get a pointer to a shared -* memory object (and increase the ref count) -* from an identifier we return an error. All -* pointers that has been added in params have -* an increased ref count. It's the callers -* responibility to do tee_shm_put() on all -* resolved pointers. -*/ - shm =tee_shm_get_from_id (ctx, ip.c); -if (IS_ERR (shm)) -return PTR_ERR (shm); - -/* -* Ensure offset + size does not overflow -* offset and does not overflow the size of -* the referred shared memory object. -*/ -if ((ip.a + ip.b) < ip.a || - (ip.a + ip.b) > shm->size) { -tee_shm_put (shm); -return -EINVAL ; - } - }else if (ctx->cap_memref_null) { -/* Pass NULL pointer to OP-TEE */ - shm =NULL ; - }else { -return -EINVAL ; - } - - params[n].u.memref.shm_offs = ip.a; - params[n].u.memref.size = ip.b; - params[n].u.memref.shm = shm; + rc =param_from_user_memref (ctx, ¶ms[n].u.memref, + &ip); +if (rc) +return rc;break ;default :/* Unknown attribute */ Signature · 1 line
2.43.0
PATCH v8 8/14 tee: new ioctl to a register tee_shm from a dmabuf file descriptor
In reply to Jens WiklanderFrom: Etienne Carriere <[email protected]>
Add a userspace API to create a tee_shm object that refers to a dmabuf reference.
Userspace registers the dmabuf file descriptor as in a tee_shm object. The registration is completed with a tee_shm returned file descriptor.
Userspace is free to close the dmabuf file descriptor after it has been registered since all the resources are now held via the new tee_shm object.
Closing the tee_shm file descriptor will eventually release all resources used by the tee_shm object when all references are released.
The new IOCTL, TEE_IOC_SHM_REGISTER_FD, supports dmabuf references to physically contiguous memory buffers. Dmabuf references acquired from the TEE DMA-heap can be used as protected memory for Secure Video Path and such use cases. It depends on the TEE and the TEE driver if dmabuf references acquired by other means can be used.
A new tee_shm flag is added to identify tee_shm objects built from a registered dmabuf, TEE_SHM_DMA_BUF.
--- drivers/tee/tee_core.c | 63 +++++++++++++++++++++- drivers/tee/tee_private.h | 10 ++++ drivers/tee/tee_shm.c | 111 ++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 1 + include/linux/tee_drv.h | 10 ++++ include/uapi/linux/tee.h | 31 +++++++++++ 6 files changed, 221 insertions(+), 5 deletions(-)
drivers/tee/tee_core.c +62 -1
@@ -353,11 +353,49 @@ tee_ioctl_shm_register(struct tee_context *ctx,return ret; } +static int +tee_ioctl_shm_register_fd (struct tee_context *ctx, +struct tee_ioctl_shm_register_fd_data __user *udata) +{ +struct tee_ioctl_shm_register_fd_data data; +struct tee_shm *shm; +long ret; + +if (copy_from_user (&data, udata,sizeof (data))) +return -EFAULT ; + +/* Currently no input flags are supported */ +if (data.flags) +return -EINVAL ; + + shm =tee_shm_register_fd (ctx, data.fd); +if (IS_ERR (shm)) +return -EINVAL ; + + data.id = shm->id; + data.flags = shm->flags; + data.size = shm->size; + +if (copy_to_user (udata, &data,sizeof (data))) + ret = -EFAULT ; +else + ret =tee_shm_get_fd (shm); + +/* +* When user space closes the file descriptor the shared memory +* should be freed or if tee_shm_get_fd() failed then it will +* be freed immediately. +*/ +tee_shm_put (shm); +return ret; +} +static int param_from_user_memref (struct tee_context *ctx,struct tee_param_memref *memref,struct tee_ioctl_param *ip) {struct tee_shm *shm; +size_t offs =0 ;/* * If a NULL pointer is passed to a TA in the TEE,
@@ -388,6 +426,26 @@ static int param_from_user_memref(struct tee_context *ctx,tee_shm_put (shm);return -EINVAL ; } + +if (shm->flags &TEE_SHM_DMA_BUF ) { +struct tee_shm_dmabuf_ref *ref; + + ref = container_of(shm, struct tee_shm_dmabuf_ref, shm); +if (ref->parent_shm) { +/* +* The shm already has one reference to +* ref->parent_shm so we are clear of 0. +* We're getting another reference since +* this shm will be used in the parameter +* list instead of the shm we got with +* tee_shm_get_from_id() above. +*/ +refcount_inc (&ref->parent_shm->refcount); +tee_shm_put (shm); + shm = ref->parent_shm; + offs = ref->offset; + } + } }else if (ctx->cap_memref_null) {/* Pass NULL pointer to OP-TEE */ shm =NULL ;
@@ -395,7 +453,7 @@ static int param_from_user_memref(struct tee_context *ctx,return -EINVAL ; } - memref->shm_offs = ip->a; + memref->shm_offs = ip->a + offs; memref->size = ip->b; memref->shm = shm;
@@ -841,6 +899,8 @@ static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return tee_ioctl_shm_alloc (ctx, uarg);case TEE_IOC_SHM_REGISTER :return tee_ioctl_shm_register (ctx, uarg); +case TEE_IOC_SHM_REGISTER_FD : +return tee_ioctl_shm_register_fd (ctx, uarg);case TEE_IOC_OPEN_SESSION :return tee_ioctl_open_session (ctx, uarg);case TEE_IOC_INVOKE :
@@ -1302,3 +1362,4 @@ MODULE_AUTHOR("Linaro");MODULE_DESCRIPTION ("TEE Driver" );MODULE_VERSION ("1.0" );MODULE_LICENSE ("GPL v2" ); +MODULE_IMPORT_NS ("DMA_BUF" );
drivers/tee/tee_private.h +10 -0
@@ -13,6 +13,16 @@#include <linux/mutex.h> #include <linux/types.h> +/* extra references appended to shm object for registered shared memory */ +struct tee_shm_dmabuf_ref { +struct tee_shm shm; +size_t offset; +struct dma_buf *dmabuf; +struct dma_buf_attachment *attach; +struct sg_table *sgt; +struct tee_shm *parent_shm; +}; +int tee_shm_get_fd (struct tee_shm *shm);bool tee_device_get (struct tee_device *teedev);
drivers/tee/tee_shm.c +107 -4
142 lines not on this page · raw message
include/linux/tee_core.h +1 -0
@@ -28,6 +28,7 @@#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 */ +#define TEE_SHM_DMA_BUF BIT(4)/* Memory with dma-buf handle */ #define TEE_DEVICE_FLAG_REGISTERED 0x1#define TEE_MAX_DEV_NAME_LEN 32
include/linux/tee_drv.h +10 -0
@@ -116,6 +116,16 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);struct tee_shm *tee_shm_register_kernel_buf (struct tee_context *ctx,void *addr,size_t length); +/** +* tee_shm_register_fd() - Register shared memory from file descriptor +* +* @ctx: Context that allocates the shared memory +* @fd: Shared memory file descriptor reference +* +* @returns a pointer to 'struct tee_shm' on success, and ERR_PTR on failure +*/ +struct tee_shm *tee_shm_register_fd (struct tee_context *ctx,int fd); +/** * tee_shm_free() - Free shared memory * @shm: Handle to shared memory to free
include/uapi/linux/tee.h +31 -0
@@ -118,6 +118,37 @@ struct tee_ioctl_shm_alloc_data {#define TEE_IOC_SHM_ALLOC _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 1, \ struct tee_ioctl_shm_alloc_data) +/** +* struct tee_ioctl_shm_register_fd_data - Shared memory registering argument +* @fd: [in] File descriptor identifying dmabuf reference +* @size: [out] Size of referenced memory +* @flags: [in] Flags to/from allocation. +* @id: [out] Identifier of the shared memory +* +* The flags field should currently be zero as input. Updated by the call +* with actual flags as defined by TEE_IOCTL_SHM_* above. +* This structure is used as argument for TEE_IOC_SHM_REGISTER_FD below. +*/ +struct tee_ioctl_shm_register_fd_data { +__s64 fd; +__u64 size; +__u32 flags; +__s32 id; +}; + +/** +* TEE_IOC_SHM_REGISTER_FD - register a shared memory from a file descriptor +* +* Returns a file descriptor on success or < 0 on failure +* +* The returned file descriptor refers to the shared memory object in the +* kernel. The supplied file deccriptor can be closed if it's not needed +* for other purposes. The shared memory is freed when the descriptor is +* closed. +*/ +#define TEE_IOC_SHM_REGISTER_FD _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 8, \ + struct tee_ioctl_shm_register_fd_data) +/** * struct tee_ioctl_buf_data - Variable sized buffer * @buf_ptr: [in] A __user pointer to a buffer
Signature · 1 line
2.43.0
PATCH v8 9/14 cma: export cma_alloc() and cma_release()
In reply to Jens WiklanderExport the two functions cma_alloc() and cma_release().
--- mm/cma.c | 2 ++ 1 file changed, 2 insertions(+)
mm/cma.c +2 -0
@@ -915,6 +915,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, {return __cma_alloc (cma, count, align,GFP_KERNEL | (no_warn ? __GFP_NOWARN :0 )); } +EXPORT_SYMBOL (cma_alloc);struct folio *cma_alloc_folio (struct cma *cma,int order,gfp_t gfp) {
@@ -1002,6 +1003,7 @@ bool cma_release(struct cma *cma, const struct page *pages,return true; } +EXPORT_SYMBOL (cma_release);bool cma_free_folio (struct cma *cma,const struct folio *folio) {
Signature · 1 line
2.43.0
Re: [PATCH v8 09/14] cma: export cma_alloc() and cma_release()
Quoted 1 line · Jens Wiklander
Export the two functions
cma_alloc()andcma_release().
Why? This is clearly part of a larger series, but you've given those of us who are subscribed to linux-mm absolutely no information about why you want to do this.
Re: [PATCH v8 09/14] cma: export cma_alloc() and cma_release()
Hi,
Quoted 7 lines · Matthew Wilcox
Quoted 1 line · Jens Wiklander
Export the two functions
cma_alloc()andcma_release().Why? This is clearly part of a larger series, but you've given those of us who are subscribed to linux-mm absolutely no information about why you want to do this.
I'm sorry, it's part of this patch: set
https://lore.kernel.org/lkml/20250502100049.1746335-1-jens.wiklander(a)linaro.org/
I'll CC a bit more in the next version of the patch set.
It's needed if the TEE subsystem is built as a load module.
Thanks,
Jens
PATCH v8 10/14 dma-contiguous: export dma_contiguous_default_area
In reply to Jens WiklanderExport the global variable dma_contiguous_default_area so dev_get_cma_area() can be called a module.
--- kernel/dma/contiguous.c | 1 + 1 file changed, 1 insertion(+)
kernel/dma/contiguous.c +1 -0
@@ -53,6 +53,7 @@#endif struct cma *dma_contiguous_default_area; +EXPORT_SYMBOL (dma_contiguous_default_area);/* * Default global CMA area size can be defined in kernel's .config.
Signature · 1 line
2.43.0
Robin Murphy
reviewerRe: [PATCH v8 10/14] dma-contiguous: export dma_contiguous_default_area
Quoted 2 lines · Jens Wiklander
Export the global variable
dma_contiguous_default_areasodev_get_cma_area()can be called a module.
What dma_map_ops implementation is in a module? Without any other context, I can't help thinking this smells of something sketchy.
Thanks,
Robin.
Quoted 20 lines
Cc Marek Szyprowski <[email protected]>Cc Robin Murphy <[email protected]>Cc iommu(a)lists.linux.devSigned-off-by Jens Wiklander <[email protected]>--- kernel/dma/contiguous.c | 1 + 1 file changed, 1 insertion(+)kernel/dma/contiguous.c +1
-0@@ -53,6 +53,7 @@#endif struct cma *dma_contiguous_default_area; +EXPORT_SYMBOL (dma_contiguous_default_area);/* * Default global CMA area size can be defined in kernel's .config.
Re: [PATCH v8 10/14] dma-contiguous: export dma_contiguous_default_area
Hi,
Quoted 7 lines · Robin Murphy
Quoted 2 lines · Jens Wiklander
Export the global variable
dma_contiguous_default_areasodev_get_cma_area()can be called a module.What
dma_map_opsimplementation is in a module? Without any other context, I can't help thinking this smells of something sketchy.
I see that you found the rest of the patch set
https://lore.kernel.org/lkml/20250502100049.1746335-1-jens.wiklander(a)linaro.org/
Thanks,
Jens
Quoted 24 lines
Thanks,
Robin.Quoted 20 lines
Cc Marek Szyprowski <[email protected]>Cc Robin Murphy <[email protected]>Cc iommu(a)lists.linux.devSigned-off-by Jens Wiklander <[email protected]>--- kernel/dma/contiguous.c | 1 + 1 file changed, 1 insertion(+)kernel/dma/contiguous.c +1
-0@@ -53,6 +53,7 @@#endif struct cma *dma_contiguous_default_area; +EXPORT_SYMBOL (dma_contiguous_default_area);/* * Default global CMA area size can be defined in kernel's .config.
PATCH v8 11/14 tee: add tee_shm_alloc_cma_phys_mem()
In reply to Jens WiklanderAdd tee_shm_alloc_cma_phys_mem() to allocate a physical memory using from the default CMA pool. The memory is represented by a tee_shm object using the new flag TEE_SHM_CMA_BUF to identify it as physical memory from CMA.
--- drivers/tee/tee_shm.c | 55 ++++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 4 +++ 2 files changed, 57 insertions(+), 2 deletions(-)
drivers/tee/tee_shm.c +53 -2
@@ -3,8 +3,11 @@* Copyright (c) 2015-2017, 2019-2021 Linaro Limited */ #include <linux/anon_inodes.h> +#include <linux/cma.h> #include <linux/device.h> #include <linux/dma-buf.h> +#include <linux/dma-map-ops.h> +#include <linux/highmem.h> #include <linux/idr.h> #include <linux/io.h> #include <linux/mm.h>
@@ -13,7 +16,6 @@#include <linux/tee_core.h> #include <linux/uaccess.h> #include <linux/uio.h> -#include <linux/highmem.h> #include "tee_private.h" static void shm_put_kernel_pages (struct page **pages,size_t page_count)
@@ -49,7 +51,14 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)struct tee_shm *parent_shm =NULL ;void *p = shm; -if (shm->flags &TEE_SHM_DMA_BUF ) { +if (shm->flags &TEE_SHM_CMA_BUF ) { +#if IS_ENABLED (CONFIG_CMA ) +struct page *page =phys_to_page (shm->paddr); +struct cma *cma =dev_get_cma_area (&shm->ctx->teedev->dev); + +cma_release (cma, page, shm->size /PAGE_SIZE ); +#endif + }else if (shm->flags &TEE_SHM_DMA_BUF ) {struct tee_shm_dmabuf_ref *ref; ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
@@ -306,6 +315,48 @@ 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_alloc_cma_phys_mem (struct tee_context *ctx, +size_t page_count,size_t align) +{ +#if IS_ENABLED (CONFIG_CMA ) +struct tee_device *teedev = ctx->teedev; +struct cma *cma =dev_get_cma_area (&teedev->dev); +struct tee_shm *shm; +struct page *page; + +if (!tee_device_get (teedev)) +return ERR_PTR (-EINVAL ); + + page =cma_alloc (cma, page_count, align, true/*no_warn*/ ); +if (!page) + goto err_put_teedev; + + shm =kzalloc (sizeof (*shm),GFP_KERNEL ); +if (!shm) + goto err_cma_crelease; + +refcount_set (&shm->refcount,1 ); + shm->ctx = ctx; + shm->paddr =page_to_phys (page); + shm->size = page_count *PAGE_SIZE ; + shm->flags =TEE_SHM_CMA_BUF ; + +teedev_ctx_get (ctx); + +return shm; + +err_cma_crelease: +cma_release (cma, page, page_count); +err_put_teedev: +tee_device_put (teedev); + +return ERR_PTR (-ENOMEM ); +#else +return ERR_PTR (-EINVAL ); +#endif +} +EXPORT_SYMBOL_GPL (tee_shm_alloc_cma_phys_mem); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
include/linux/tee_core.h +4 -0
@@ -29,6 +29,7 @@#define TEE_SHM_POOL BIT(2)/* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3)/* Memory private to TEE driver */ #define TEE_SHM_DMA_BUF BIT(4)/* Memory with dma-buf handle */ +#define TEE_SHM_CMA_BUF BIT(5)/* CMA allocated memory */ #define TEE_DEVICE_FLAG_REGISTERED 0x1#define TEE_MAX_DEV_NAME_LEN 32
@@ -310,6 +311,9 @@ 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_cma_phys_mem (struct tee_context *ctx, +size_t page_count,size_t align); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
Signature · 1 line
2.43.0
Robin Murphy
reviewerRe: [PATCH v8 11/14] tee: add tee_shm_alloc_cma_phys_mem()
Quoted 4 lines · Jens Wiklander
Add
tee_shm_alloc_cma_phys_mem()to allocate a physical memory using from the default CMA pool. The memory is represented by atee_shmobject using the new flagTEE_SHM_CMA_BUFto identify it as physical memory from CMA.
If and when it's possible to dynamically delegate any old kernel memory to the TEE, it's far from clear why that should involve poking around in CMA internals...
Quoted 31 lines
Signed-off-by Jens Wiklander <[email protected]>--- drivers/tee/tee_shm.c | 55 ++++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 4 +++ 2 files changed, 57 insertions(+), 2 deletions(-)drivers/tee/tee_shm.c +3
-1@@ -3,8 +3,11 @@* Copyright (c) 2015-2017, 2019-2021 Linaro Limited */ #include <linux/anon_inodes.h> +#include <linux/cma.h> #include <linux/device.h> #include <linux/dma-buf.h> +#include <linux/dma-map-ops.h> +#include <linux/highmem.h> #include <linux/idr.h> #include <linux/io.h> #include <linux/mm.h> @@ -13,7 +16,6 @@#include <linux/tee_core.h> #include <linux/uaccess.h> #include <linux/uio.h> -#include <linux/highmem.h> #include "tee_private.h" static void shm_put_kernel_pages (struct page **pages,size_t page_count)
drivers/tee/tee_shm.c static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) · line 57
Quoted 8 lines
drivers/tee/tee_shm.c +4
-1@@ -49,7 +51,14 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)struct tee_shm *parent_shm =NULL ;void *p = shm; -if (shm->flags &TEE_SHM_DMA_BUF ) { +if (shm->flags &TEE_SHM_CMA_BUF ) { +#if IS_ENABLED (CONFIG_CMA ) +struct page *page =phys_to_page (shm->paddr); +struct cma *cma =dev_get_cma_area (&shm->ctx->teedev->dev);
If you want dma_contiguous_default_area as the commit message implies, use dma_contiguous_default_area. Appearing to support per-device CMA pools but relying on the device not having one is pretty yucky.
But again, why? If you want page-backed DMA-able memory, with all the other assumptions being made here, you may as well just rely on dma_alloc_pages(DMA_ATTR_SKIP_CPU_SYNC) doing what you want, while also being potentially more flexible for !CMA and non-invasive. Or at the very least, could the TEE delegation not be composed on top of the existing CMA heap allocator?
Thanks,
Robin.
Quoted 78 lines
drivers/tee/tee_shm.c +46
-0+ +cma_release (cma, page, shm->size /PAGE_SIZE ); +#endif + }else if (shm->flags &TEE_SHM_DMA_BUF ) {struct tee_shm_dmabuf_ref *ref; ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);@@ -306,6 +315,48 @@ 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_alloc_cma_phys_mem (struct tee_context *ctx, +size_t page_count,size_t align) +{ +#if IS_ENABLED (CONFIG_CMA ) +struct tee_device *teedev = ctx->teedev; +struct cma *cma =dev_get_cma_area (&teedev->dev); +struct tee_shm *shm; +struct page *page; + +if (!tee_device_get (teedev)) +return ERR_PTR (-EINVAL ); + + page =cma_alloc (cma, page_count, align, true/*no_warn*/ ); +if (!page) + goto err_put_teedev; + + shm =kzalloc (sizeof (*shm),GFP_KERNEL ); +if (!shm) + goto err_cma_crelease; + +refcount_set (&shm->refcount,1 ); + shm->ctx = ctx; + shm->paddr =page_to_phys (page); + shm->size = page_count *PAGE_SIZE ; + shm->flags =TEE_SHM_CMA_BUF ; + +teedev_ctx_get (ctx); + +return shm; + +err_cma_crelease: +cma_release (cma, page, page_count); +err_put_teedev: +tee_device_put (teedev); + +return ERR_PTR (-ENOMEM ); +#else +return ERR_PTR (-EINVAL ); +#endif +} +EXPORT_SYMBOL_GPL (tee_shm_alloc_cma_phys_mem); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,include/linux/tee_core.h +4
-0@@ -29,6 +29,7 @@#define TEE_SHM_POOL BIT(2)/* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3)/* Memory private to TEE driver */ #define TEE_SHM_DMA_BUF BIT(4)/* Memory with dma-buf handle */ +#define TEE_SHM_CMA_BUF BIT(5)/* CMA allocated memory */ #define TEE_DEVICE_FLAG_REGISTERED 0x1#define TEE_MAX_DEV_NAME_LEN 32@@ -310,6 +311,9 @@ 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_cma_phys_mem (struct tee_context *ctx, +size_t page_count,size_t align); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
Re: [PATCH v8 11/14] tee: add tee_shm_alloc_cma_phys_mem()
Hi,
Quoted 55 lines · Robin Murphy
Quoted 4 lines · Jens Wiklander
Add
tee_shm_alloc_cma_phys_mem()to allocate a physical memory using from the default CMA pool. The memory is represented by atee_shmobject using the new flagTEE_SHM_CMA_BUFto identify it as physical memory from CMA.If and when it's possible to dynamically delegate any old kernel memory to the TEE, it's far from clear why that should involve poking around in CMA internals...
Quoted 31 lines
Signed-off-by Jens Wiklander <[email protected]>--- drivers/tee/tee_shm.c | 55 ++++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 4 +++ 2 files changed, 57 insertions(+), 2 deletions(-)drivers/tee/tee_shm.c +3
-1@@ -3,8 +3,11 @@* Copyright (c) 2015-2017, 2019-2021 Linaro Limited */ #include <linux/anon_inodes.h> +#include <linux/cma.h> #include <linux/device.h> #include <linux/dma-buf.h> +#include <linux/dma-map-ops.h> +#include <linux/highmem.h> #include <linux/idr.h> #include <linux/io.h> #include <linux/mm.h> @@ -13,7 +16,6 @@#include <linux/tee_core.h> #include <linux/uaccess.h> #include <linux/uio.h> -#include <linux/highmem.h> #include "tee_private.h" static void shm_put_kernel_pages (struct page **pages,size_t page_count)drivers/tee/tee_shm.c static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) · line 57
Quoted 8 lines
drivers/tee/tee_shm.c +4
-1@@ -49,7 +51,14 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)struct tee_shm *parent_shm =NULL ;void *p = shm; -if (shm->flags &TEE_SHM_DMA_BUF ) { +if (shm->flags &TEE_SHM_CMA_BUF ) { +#if IS_ENABLED (CONFIG_CMA ) +struct page *page =phys_to_page (shm->paddr); +struct cma *cma =dev_get_cma_area (&shm->ctx->teedev->dev);If you want
dma_contiguous_default_areaas the commit message implies, usedma_contiguous_default_area. Appearing to support per-device CMA pools but relying on the device not having one is pretty yucky.
Quoted 6 lines
But again, why? If you want page-backed DMA-able memory, with all the other assumptions being made here, you may as well just rely on
dma_alloc_pages(DMA_ATTR_SKIP_CPU_SYNC)doing what you want, while also being potentially more flexible for !CMA and non-invasive. Or at the very least, could the TEE delegation not be composed on top of the existing CMA heap allocator?
Thanks for suggesting dma_alloc_pages(). I'll use that in the next version of the patch set.
Cheers,
Jens
Quoted 82 lines
Thanks,
Robin.Quoted 78 lines
drivers/tee/tee_shm.c +46
-0+ +cma_release (cma, page, shm->size /PAGE_SIZE ); +#endif + }else if (shm->flags &TEE_SHM_DMA_BUF ) {struct tee_shm_dmabuf_ref *ref; ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);@@ -306,6 +315,48 @@ 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_alloc_cma_phys_mem (struct tee_context *ctx, +size_t page_count,size_t align) +{ +#if IS_ENABLED (CONFIG_CMA ) +struct tee_device *teedev = ctx->teedev; +struct cma *cma =dev_get_cma_area (&teedev->dev); +struct tee_shm *shm; +struct page *page; + +if (!tee_device_get (teedev)) +return ERR_PTR (-EINVAL ); + + page =cma_alloc (cma, page_count, align, true/*no_warn*/ ); +if (!page) + goto err_put_teedev; + + shm =kzalloc (sizeof (*shm),GFP_KERNEL ); +if (!shm) + goto err_cma_crelease; + +refcount_set (&shm->refcount,1 ); + shm->ctx = ctx; + shm->paddr =page_to_phys (page); + shm->size = page_count *PAGE_SIZE ; + shm->flags =TEE_SHM_CMA_BUF ; + +teedev_ctx_get (ctx); + +return shm; + +err_cma_crelease: +cma_release (cma, page, page_count); +err_put_teedev: +tee_device_put (teedev); + +return ERR_PTR (-ENOMEM ); +#else +return ERR_PTR (-EINVAL ); +#endif +} +EXPORT_SYMBOL_GPL (tee_shm_alloc_cma_phys_mem); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,include/linux/tee_core.h +4
-0@@ -29,6 +29,7 @@#define TEE_SHM_POOL BIT(2)/* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3)/* Memory private to TEE driver */ #define TEE_SHM_DMA_BUF BIT(4)/* Memory with dma-buf handle */ +#define TEE_SHM_CMA_BUF BIT(5)/* CMA allocated memory */ #define TEE_DEVICE_FLAG_REGISTERED 0x1#define TEE_MAX_DEV_NAME_LEN 32@@ -310,6 +311,9 @@ 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_cma_phys_mem (struct tee_context *ctx, +size_t page_count,size_t align); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
PATCH v8 12/14 optee: support protected memory allocation
In reply to Jens WiklanderAdd support in the OP-TEE backend driver for protected memory allocation. The support is limited to only the SMC ABI and for secure video buffers.
OP-TEE is probed for the range of protected physical memory and a memory pool allocator is initialized if OP-TEE have support for such memory.
--- drivers/tee/optee/core.c | 1 + drivers/tee/optee/smc_abi.c | 44 +++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-)
drivers/tee/optee/core.c +1 -0
@@ -181,6 +181,7 @@ void optee_remove_common(struct optee *optee)tee_device_unregister (optee->supp_teedev);tee_device_unregister (optee->teedev); +tee_device_unregister_all_dma_heaps (optee->teedev);tee_shm_pool_free (optee->pool);optee_supp_uninit (&optee->supp);mutex_destroy (&optee->call_queue.mutex);
drivers/tee/optee/smc_abi.c +42 -2
74 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 13/14 optee: FF-A: dynamic protected memory allocation
In reply to Jens WiklanderAdd support in the OP-TEE backend driver dynamic protected memory allocation with FF-A.
The protected memory pools for dynamically allocated protected memory are instantiated when requested by user-space. This instantiation can fail if OP-TEE doesn't support the requested use-case of protected memory.
Restricted memory pools based on a static carveout or dynamic allocation can coexist for different use-cases. We use only dynamic allocation with FF-A.
--- drivers/tee/optee/Makefile | 1 + drivers/tee/optee/ffa_abi.c | 147 ++++++++++++- drivers/tee/optee/optee_private.h | 13 +- drivers/tee/optee/protmem.c | 330 ++++++++++++++++++++++++++++++ 4 files changed, 488 insertions(+), 3 deletions(-) create mode 100644 drivers/tee/optee/protmem.c
drivers/tee/optee/Makefile +1 -0
@@ -4,6 +4,7 @@ optee-objs += core.o optee-objs += call.o optee-objs += notif.o optee-objs += rpc.o +optee-objs += protmem.o optee-objs += supp.o optee-objs += device.o optee-objs += smc_abi.o
drivers/tee/optee/ffa_abi.c +145 -2
189 lines not on this page · raw message
drivers/tee/optee/optee_private.h +12 -1
@@ -174,9 +174,14 @@ struct optee;* @do_call_with_arg: enters OP-TEE in secure world * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param +* @lend_protmem: lends physically contiguous memory as restricted +* memory, inaccessible by the kernel +* @reclaim_protmem: reclaims restricted memory previously lent with +* @lend_protmem() and makes it accessible by the +* kernel again * * These OPs are only supposed to be used internally in the OP-TEE driver -* as a way of abstracting the different methogs of entering OP-TEE in +* as a way of abstracting the different methods of entering OP-TEE in * secure world. */ struct optee_ops {
@@ -191,6 +196,10 @@ struct optee_ops {size_t num_params, const struct optee_msg_param *msg_params, bool update_out); +int (*lend_protmem)(struct optee *optee,struct tee_shm *protmem, +u16 *end_points,unsigned int ep_count, +u32 use_case); +int (*reclaim_protmem)(struct optee *optee,struct tee_shm *protmem); };/**
@@ -285,6 +294,8 @@ u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,void optee_supp_init (struct optee_supp *supp);void optee_supp_uninit (struct optee_supp *supp);void optee_supp_release (struct optee_supp *supp); +struct tee_protmem_pool *optee_protmem_alloc_cma_pool (struct optee *optee, +enum tee_dma_heap_id id);int optee_supp_recv (struct tee_context *ctx,u32 *func,u32 *num_params,struct tee_param *param);
drivers/tee/optee/protmem.c new file +330 -0
330 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 14/14 optee: smc abi: dynamic protected memory allocation
In reply to Jens WiklanderAdd support in the OP-TEE backend driver for dynamic protected memory allocation using the SMC ABI.
--- drivers/tee/optee/smc_abi.c | 103 +++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 18 deletions(-)
drivers/tee/optee/smc_abi.c +85 -18
@@ -1001,6 +1001,70 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx,return rc; } +static int optee_smc_lend_protmem (struct optee *optee,struct tee_shm *protmem, +u16 *end_points,unsigned int ep_count, +u32 use_case) +{ +struct optee_shm_arg_entry *entry; +struct optee_msg_arg *msg_arg; +struct tee_shm *shm; +u_int offs; +int rc; + + msg_arg =optee_get_msg_arg (optee->ctx,2 , &entry, &shm, &offs); +if (IS_ERR (msg_arg)) +return PTR_ERR (msg_arg); + + msg_arg->cmd =OPTEE_MSG_CMD_LEND_PROTMEM ; + msg_arg->params[0 ].attr =OPTEE_MSG_ATTR_TYPE_VALUE_INPUT ; + msg_arg->params[0 ].u.value.a = use_case; + msg_arg->params[1 ].attr =OPTEE_MSG_ATTR_TYPE_TMEM_INPUT ; + msg_arg->params[1 ].u.tmem.buf_ptr = protmem->paddr; + msg_arg->params[1 ].u.tmem.size = protmem->size; + msg_arg->params[1 ].u.tmem.shm_ref = (u_long )protmem; + + rc = optee->ops->do_call_with_arg (optee->ctx, shm, offs, false); +if (rc) + goto out; +if (msg_arg->ret !=TEEC_SUCCESS ) { + rc = -EINVAL ; + goto out; + } + protmem->sec_world_id = (u_long )protmem; + +out: +optee_free_msg_arg (optee->ctx, entry, offs); +return rc; +} + +static int optee_smc_reclaim_protmem (struct optee *optee, +struct tee_shm *protmem) +{ +struct optee_shm_arg_entry *entry; +struct optee_msg_arg *msg_arg; +struct tee_shm *shm; +u_int offs; +int rc; + + msg_arg =optee_get_msg_arg (optee->ctx,1 , &entry, &shm, &offs); +if (IS_ERR (msg_arg)) +return PTR_ERR (msg_arg); + + msg_arg->cmd =OPTEE_MSG_CMD_RECLAIM_PROTMEM ; + msg_arg->params[0 ].attr =OPTEE_MSG_ATTR_TYPE_RMEM_INPUT ; + msg_arg->params[0 ].u.rmem.shm_ref = (u_long )protmem; + + rc = optee->ops->do_call_with_arg (optee->ctx, shm, offs, false); +if (rc) + goto out; +if (msg_arg->ret !=TEEC_SUCCESS ) + rc = -EINVAL ; + +out: +optee_free_msg_arg (optee->ctx, entry, offs); +return rc; +} +/* * 5. Asynchronous notification */
@@ -1252,6 +1316,8 @@ static const struct optee_ops optee_ops = { .do_call_with_arg = optee_smc_do_call_with_arg, .to_msg_param = optee_to_msg_param, .from_msg_param = optee_from_msg_param, + .lend_protmem = optee_smc_lend_protmem, + .reclaim_protmem = optee_smc_reclaim_protmem, };static int enable_async_notif (optee_invoke_fn *invoke_fn)
@@ -1622,11 +1688,14 @@ static inline int optee_load_fw(struct platform_device *pdev,static int optee_protmem_pool_init (struct optee *optee) { +bool protm = optee->smc.sec_caps &OPTEE_SMC_SEC_CAP_PROTMEM ; +bool dyn_protm = optee->smc.sec_caps & +OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM ;enum tee_dma_heap_id heap_id =TEE_DMA_HEAP_SECURE_VIDEO_PLAY ; -struct tee_protmem_pool *pool; -int rc; +struct tee_protmem_pool *pool =ERR_PTR (-EINVAL ); +int rc = -EINVAL ; -if (optee->smc.sec_caps &OPTEE_SMC_SEC_CAP_PROTMEM ) { +if (protm) {union {struct arm_smccc_res smccc;struct optee_smc_get_protmem_config_result result;
@@ -1634,25 +1703,24 @@ static int optee_protmem_pool_init(struct optee *optee) optee->smc.invoke_fn (OPTEE_SMC_GET_PROTMEM_CONFIG ,0 ,0 ,0 ,0 ,0 ,0 ,0 , &res.smccc); -if (res.result.status !=OPTEE_SMC_RETURN_OK ) { -pr_err ("Secure Data Path service not available\n" ); -return 0 ; - } +if (res.result.status ==OPTEE_SMC_RETURN_OK ) + pool =tee_protmem_static_pool_alloc (res.result.start, + res.result.size); + } - pool =tee_protmem_static_pool_alloc (res.result.start, - res.result.size); -if (IS_ERR (pool)) -return PTR_ERR (pool); +if (dyn_protm &&IS_ERR (pool)) + pool =optee_protmem_alloc_cma_pool (optee, heap_id); +if (!IS_ERR (pool)) { rc =tee_device_register_dma_heap (optee->teedev, heap_id, pool);if (rc) - goto err; + pool->ops->destroy_pool (pool); } +if (protm || dyn_protm) +return rc; +return 0 ; -err: - pool->ops->destroy_pool (pool); -return rc; }static int optee_probe (struct platform_device *pdev)
@@ -1823,9 +1891,8 @@ static int optee_probe(struct platform_device *pdev)pr_info ("Asynchronous notifications enabled\n" ); } - rc =optee_protmem_pool_init (optee); -if (rc) - goto err_notif_uninit; +if (optee_protmem_pool_init (optee)) +pr_info ("Protected memory service not available\n" );/* * Ensure that there are no pre-existing shm objects before enabling
Signature · 1 line
2.43.0