TEE subsystem for protected dma-buf allocations
Subsystems (5)
v12 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) have been identified so far to serve as examples of what can be expected. The use-cases have 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 its own protected memory pool since different use-cases require 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 (dma_alloc_pages()) 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-v12
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 sets can be tested on RockPi in the same way using:
xtest --sdp-basic
The primitive tests 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-v12
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 to SPMC_AT_EL=n to test without FF-A using the original SMC ABI instead. Please remember to do %make arm-tf-clean
for TF-A to be rebuilt properly using the new configuration.
https://optee.readthedocs.io/en/latest/building/prerequisites.html
list dependencies required to build the above.
The primitive 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 V11:
* In "dma-buf: dma-heap: export declared functions":
- use EXPORT_SYMBOL_NS_GPL()
- Added TJ's R-B and Sumit's Ack
* In "tee: implement protected DMA-heap", import the namespaces "DMA_BUF" and "DMA_BUF_HEAP" as needed.
Changes since V10:
* Changed the new ABI OPTEE_MSG_CMD_GET_PROTMEM_CONFIG to report a list of u32 memory attributes instead of u16 endpoints to make room for both endpoint and access permissions in each entry.
* In "tee: new ioctl to a register tee_shm from a dmabuf file descriptor", remove the unused path for DMA-bufs allocated by other means than the on in the TEE SS.
* In "tee: implement protected DMA-heap", handle unloading of the backend driver module implementing the heap. The heap is reference counted and also calls tee_device_get() to guarantee that the module remains available while the heap is instantiated.
* In "optee: support protected memory allocation", use dma_coerce_mask_and_coherent() instead of open-coding the function.
* Added Sumit's R-B to
- "optee: smc abi: dynamic protected memory allocation"
- "optee: FF-A: dynamic protected memory allocation"
- "optee: support protected memory allocation"
- "tee: implement protected DMA-heap"
- "dma-buf: dma-heap: export declared functions"
Changes since V9:
* Adding Sumit's R-B to "optee: sync secure world ABI headers"
* Update commit message as requested for "dma-buf: dma-heap: export declared functions".
* In "tee: implement protected DMA-heap":
- add the hidden config option TEE_DMABUF_HEAPS to tell if the TEE subsystem can support DMA heaps
- add a pfn_valid() to check that the passed physical address can be used by __pfn_to_page() and friends
- remove the memremap() call, the caller is should do that instead if needed
* In "tee: add tee_shm_alloc_dma_mem()" guard the calls to dma_alloc_pages() and dma_free_pages() with TEE_DMABUF_HEAPS to avoid linking errors in some configurations
* In "optee: support protected memory allocation":
- add the hidden config option OPTEE_STATIC_PROTMEM_POOL to tell if the driver can support a static protected memory pool
- optee_protmem_pool_init() is slightly refactored to make the patches that follow easier
- Call devm_memremap() before calling tee_protmem_static_pool_alloc()
Changes since V8:
* Using dma_alloc_pages() instead of cma_alloc() so the direct dependency on CMA can be removed together with the patches
"cma: export cma_alloc() and cma_release()" and "dma-contiguous: export dma_contiguous_default_area". The patch
* Renaming the patch "tee: add tee_shm_alloc_cma_phys_mem()" to "tee: add tee_shm_alloc_dma_mem()"
* Setting DMA mask for the OP-TEE TEE device based on input from the secure world instead of relying on the parent device so following patches are
removed: "tee: tee_device_alloc(): copy dma_mask from parent device" and "optee: pass parent device to tee_device_alloc()".
* Adding Sumit Garg's R-B to "tee: refactor params_from_user()"
* In the patch "tee: implement protected DMA-heap", map the physical memory passed to tee_protmem_static_pool_alloc().
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.
* Removing the patch "optee: account for direction while converting parameters"
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 [email protected]
* 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 (8):
optee: sync secure world ABI headers
dma-buf: dma-heap: export declared functions
tee: implement protected DMA-heap
tee: refactor params_from_user()
tee: add tee_shm_alloc_dma_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 | 4 + drivers/tee/Kconfig | 5 + drivers/tee/Makefile | 1 + drivers/tee/optee/Kconfig | 5 + drivers/tee/optee/Makefile | 1 + drivers/tee/optee/core.c | 7 + drivers/tee/optee/ffa_abi.c | 146 ++++++++- drivers/tee/optee/optee_ffa.h | 27 +- drivers/tee/optee/optee_msg.h | 84 ++++- drivers/tee/optee/optee_private.h | 15 +- drivers/tee/optee/optee_smc.h | 37 ++- drivers/tee/optee/protmem.c | 335 ++++++++++++++++++++ drivers/tee/optee/smc_abi.c | 141 ++++++++- drivers/tee/tee_core.c | 158 +++++++--- drivers/tee/tee_heap.c | 500 ++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 14 + drivers/tee/tee_shm.c | 157 +++++++++- include/linux/tee_core.h | 59 ++++ include/linux/tee_drv.h | 10 + include/uapi/linux/tee.h | 31 ++ 20 files changed, 1670 insertions(+), 67 deletions(-) create mode 100644 drivers/tee/optee/protmem.c create mode 100644 drivers/tee/tee_heap.c
base-commit: c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9
Signature · 1 line
2.43.0
PATCH v12 1/9 optee: sync secure world ABI headers
Update 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 | 84 ++++++++++++++++++++++++++++++----- drivers/tee/optee/optee_smc.h | 37 ++++++++++++++- 3 files changed, 130 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 +72 -12
113 lines not on this page · raw message
drivers/tee/optee/optee_smc.h +36 -1
@@ -264,7 +264,6 @@ struct optee_smc_get_shm_config_result {#define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM BIT(0)/* Secure world can communicate via previously unregistered shared memory */ #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM BIT(1) -/* * Secure world supports commands "register/unregister shared memory", * secure world accepts command buffers located in any parts of non-secure RAM
@@ -280,6 +279,10 @@ struct optee_smc_get_shm_config_result {#define OPTEE_SMC_SEC_CAP_RPC_ARG BIT(6)/* Secure world supports probing for RPMB device if needed */ #define OPTEE_SMC_SEC_CAP_RPMB_PROBE BIT(7) +/* Secure world supports protected memory */ +#define OPTEE_SMC_SEC_CAP_PROTMEM BIT(8) +/* Secure world supports dynamic protected memory */ +#define OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM BIT(9)#define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9#define OPTEE_SMC_EXCHANGE_CAPABILITIES \
@@ -451,6 +454,38 @@ struct optee_smc_disable_shm_cache_result {/* See OPTEE_SMC_CALL_WITH_REGD_ARG above */ #define OPTEE_SMC_FUNCID_CALL_WITH_REGD_ARG 19 +/* +* Get protected memory config +* +* Returns the protected memory config. +* +* Call register usage: +* a0 SMC Function ID, OPTEE_SMC_GET_PROTMEM_CONFIG +* a2-6 Not used, must be zero +* a7 Hypervisor Client ID register +* +* Have config return register usage: +* a0 OPTEE_SMC_RETURN_OK +* a1 Physical address of start of protected memory +* a2 Size of protected memory +* a3 PA width, max 64 +* a4-7 Preserved +* +* Not available register usage: +* a0 OPTEE_SMC_RETURN_ENOTAVAIL +* a1-3 Not used +* a4-7 Preserved +*/ +#define OPTEE_SMC_FUNCID_GET_PROTMEM_CONFIG 20 +#define OPTEE_SMC_GET_PROTMEM_CONFIG \ + OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_PROTMEM_CONFIG) + +struct optee_smc_get_protmem_config_result { +unsigned long status; +unsigned long start; +unsigned long size; +unsigned long pa_width; +};/* * Resume from RPC (for example after processing a foreign interrupt)
Signature · 1 line
2.43.0
PATCH v12 2/9 dma-buf: dma-heap: export declared functions
In reply to Jens WiklanderExport the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it.
--- drivers/dma-buf/dma-heap.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/dma-buf/dma-heap.c +4 -0
@@ -11,6 +11,7 @@#include <linux/dma-buf.h> #include <linux/dma-heap.h> #include <linux/err.h> +#include <linux/export.h> #include <linux/list.h> #include <linux/nospec.h> #include <linux/syscalls.h>
@@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) {return heap->priv; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_drvdata,"DMA_BUF_HEAP" );/** * dma_heap_get_name - get heap name
@@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) {return heap->name; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_name,"DMA_BUF_HEAP" );/** * dma_heap_add - adds a heap to dmabuf heaps
@@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)kfree (heap);return err_ret; } +EXPORT_SYMBOL_NS_GPL (dma_heap_add,"DMA_BUF_HEAP" );static char *dma_heap_devnode (const struct device *dev,umode_t *mode) {
Signature · 1 line
2.43.0
Re: [PATCH v12 2/9] dma-buf: dma-heap: export declared functions
Hi,
Quoted 41 lines · Jens Wiklander
Export the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it.
Reviewed-by Sumit Garg <[email protected]>Reviewed-by T.J. Mercier <[email protected]>Acked-by Sumit Semwal <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- drivers/dma-buf/dma-heap.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma-buf/dma-heap.c +3
-0@@ -11,6 +11,7 @@#include <linux/dma-buf.h> #include <linux/dma-heap.h> #include <linux/err.h> +#include <linux/export.h> #include <linux/list.h> #include <linux/nospec.h> #include <linux/syscalls.h> @@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) {return heap->priv; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_drvdata,"DMA_BUF_HEAP" );/** * dma_heap_get_name - get heap name @@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) {return heap->name; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_name,"DMA_BUF_HEAP" );/** * dma_heap_add - adds a heap to dmabuf heaps
drivers/dma-buf/dma-heap.c struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) · line 309
Quoted 4 lines
drivers/dma-buf/dma-heap.c +1
-0@@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)kfree (heap);return err_ret; } +EXPORT_SYMBOL_NS_GPL (dma_heap_add,"DMA_BUF_HEAP" );
It's not clear to me why we would need to export those symbols.
As far as I know, heaps cannot be removed, and compiling them as module means that we would be able to remove them.
Now, if we don't expect the users to be compiled as modules, then we don't need to export these symbols at all.
Am I missing something?
Maxime
Re: [PATCH v12 2/9] dma-buf: dma-heap: export declared functions
Hi,
Quoted 52 lines · Maxime Ripard
Hi,
Quoted 41 lines · Jens Wiklander
Export the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it.
Reviewed-by Sumit Garg <[email protected]>Reviewed-by T.J. Mercier <[email protected]>Acked-by Sumit Semwal <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- drivers/dma-buf/dma-heap.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma-buf/dma-heap.c +3
-0@@ -11,6 +11,7 @@#include <linux/dma-buf.h> #include <linux/dma-heap.h> #include <linux/err.h> +#include <linux/export.h> #include <linux/list.h> #include <linux/nospec.h> #include <linux/syscalls.h> @@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) {return heap->priv; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_drvdata,"DMA_BUF_HEAP" );/** * dma_heap_get_name - get heap name @@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) {return heap->name; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_name,"DMA_BUF_HEAP" );/** * dma_heap_add - adds a heap to dmabuf heaps drivers/dma-buf/dma-heap.c struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) · line 309
Quoted 4 lines
drivers/dma-buf/dma-heap.c +1
-0@@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)kfree (heap);return err_ret; } +EXPORT_SYMBOL_NS_GPL (dma_heap_add,"DMA_BUF_HEAP" );It's not clear to me why we would need to export those symbols.
Quoted 7 lines
As far as I know, heaps cannot be removed, and compiling them as module means that we would be able to remove them.
Now, if we don't expect the users to be compiled as modules, then we don't need to export these symbols at all.
Am I missing something?
In this case, it's the TEE module that _might_ need to instantiate a DMA heap. Whether it will be instantiated depends on the TEE backend driver and the TEE firmware. If a heap is instantiated, then it will not be possible to unload the TEE module. That might not be perfect, but in my opinion, it's better than other options, such as always making the TEE subsystem built-in or disabling DMA-heap support when compiled as a module.
Thanks,
Jens
Re: [PATCH v12 2/9] dma-buf: dma-heap: export declared functions
In reply to Maxime RipardLe jeudi 02 octobre 2025 à 09:47 +0200, Maxime Ripard a écrit :
Quoted 52 lines
Hi,
Quoted 41 lines · Jens Wiklander
Export the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it.
Reviewed-by Sumit Garg <[email protected]>Reviewed-by T.J. Mercier <[email protected]>Acked-by Sumit Semwal <[email protected]>Signed-off-by Jens Wiklander <[email protected]>---
drivers/dma-buf/dma-heap.c | 4 ++++1 file changed, 4 insertions(+)drivers/dma-buf/dma-heap.c +2
-0@@ -11,6 +11,7 @@ #include <linux/dma-buf.h> #include <linux/dma-heap.h> #include <linux/err.h> +#include <linux/export.h> #include <linux/list.h> #include <linux/nospec.h> #include <linux/syscalls.h>@@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) { return heap->priv; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_drvdata,"DMA_BUF_HEAP" );
/**
*dma_heap_get_name- get heap namedrivers/dma-buf/dma-heap.c +1
-0@@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) { return heap->name; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_name,"DMA_BUF_HEAP" );
/**
*dma_heap_add- adds a heap to dmabuf heapsdrivers/dma-buf/dma-heap.c struct dma_heap *dma_heap_add(const struct · line 310
Quoted 5 lines
drivers/dma-buf/dma-heap.c +1
-0@@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) kfree(heap); return err_ret; } +EXPORT_SYMBOL_NS_GPL (dma_heap_add,"DMA_BUF_HEAP" );It's not clear to me why we would need to export those symbols.
Quoted 5 lines
As far as I know, heaps cannot be removed, and compiling them as module means that we would be able to remove them.
Now, if we don't expect the users to be compiled as modules, then we don't need to export these symbols at all.
Maybe I'm getting out of topic, sorry if its the case, but making that a hard rule seems very limiting. Didn't we said that a heap driver could be made to represent memory region on a remote device such as an eGPU ?
Nicolas
Quoted 4 lines
Am I missing something?
Maxime
John Stultz
reviewerRe: [PATCH v12 2/9] dma-buf: dma-heap: export declared functions
In reply to Maxime RipardQuoted 49 lines · Maxime Ripard
Quoted 41 lines · Jens Wiklander
Export the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it.
Reviewed-by Sumit Garg <[email protected]>Reviewed-by T.J. Mercier <[email protected]>Acked-by Sumit Semwal <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- drivers/dma-buf/dma-heap.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma-buf/dma-heap.c +3
-0@@ -11,6 +11,7 @@#include <linux/dma-buf.h> #include <linux/dma-heap.h> #include <linux/err.h> +#include <linux/export.h> #include <linux/list.h> #include <linux/nospec.h> #include <linux/syscalls.h> @@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) {return heap->priv; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_drvdata,"DMA_BUF_HEAP" );/** * dma_heap_get_name - get heap name @@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) {return heap->name; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_name,"DMA_BUF_HEAP" );/** * dma_heap_add - adds a heap to dmabuf heaps drivers/dma-buf/dma-heap.c struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) · line 309
Quoted 4 lines
drivers/dma-buf/dma-heap.c +1
-0@@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)kfree (heap);return err_ret; } +EXPORT_SYMBOL_NS_GPL (dma_heap_add,"DMA_BUF_HEAP" );It's not clear to me why we would need to export those symbols.
Quoted 7 lines
As far as I know, heaps cannot be removed, and compiling them as module means that we would be able to remove them.
Now, if we don't expect the users to be compiled as modules, then we don't need to export these symbols at all.
Am I missing something?
For things like distro kernels (or in Android's case, the GKI), there's a benefit for modules that can be loaded permanently (not having a module_exit hook).
One doesn't have to bloat the base kernel image/memory usage for everyone, while still not having to necessarily deal with complications from module unloading issues.
thanks
-john
Re: [PATCH v12 2/9] dma-buf: dma-heap: export declared functions
Quoted 56 lines · John Stultz
Quoted 55 lines · Maxime Ripard
Quoted 41 lines · Jens Wiklander
Export the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it.
Reviewed-by Sumit Garg <[email protected]>Reviewed-by T.J. Mercier <[email protected]>Acked-by Sumit Semwal <[email protected]>Signed-off-by Jens Wiklander <[email protected]>--- drivers/dma-buf/dma-heap.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma-buf/dma-heap.c +3
-0@@ -11,6 +11,7 @@#include <linux/dma-buf.h> #include <linux/dma-heap.h> #include <linux/err.h> +#include <linux/export.h> #include <linux/list.h> #include <linux/nospec.h> #include <linux/syscalls.h> @@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) {return heap->priv; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_drvdata,"DMA_BUF_HEAP" );/** * dma_heap_get_name - get heap name @@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) {return heap->name; } +EXPORT_SYMBOL_NS_GPL (dma_heap_get_name,"DMA_BUF_HEAP" );/** * dma_heap_add - adds a heap to dmabuf heaps drivers/dma-buf/dma-heap.c struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) · line 309
Quoted 4 lines
drivers/dma-buf/dma-heap.c +1
-0@@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)kfree (heap);return err_ret; } +EXPORT_SYMBOL_NS_GPL (dma_heap_add,"DMA_BUF_HEAP" );It's not clear to me why we would need to export those symbols.
As far as I know, heaps cannot be removed, and compiling them as module means that we would be able to remove them.
Now, if we don't expect the users to be compiled as modules, then we don't need to export these symbols at all.
Quoted 8 lines
Quoted 1 line
Am I missing something?
For things like distro kernels (or in Android's case, the GKI), there's a benefit for modules that can be loaded permanently (not having a
module_exithook).
One doesn't have to bloat the base kernel image/memory usage for everyone, while still not having to necessarily deal with complications from module unloading issues.
Ack. We should at least document it then.
Maxime
PATCH v12 3/9 tee: implement protected DMA-heap
In reply to Jens WiklanderImplement DMA heap for protected DMA-buf allocation in the TEE subsystem.
Protected 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/Kconfig | 5 + drivers/tee/Makefile | 1 + drivers/tee/tee_core.c | 4 + drivers/tee/tee_heap.c | 500 ++++++++++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 6 + include/linux/tee_core.h | 53 ++++ 6 files changed, 569 insertions(+) create mode 100644 drivers/tee/tee_heap.c
drivers/tee/Kconfig +5 -0
@@ -13,6 +13,11 @@ menuconfig TEE if TEE +config TEE_DMABUF_HEAPS + bool + depends on HAS_DMA && DMABUF_HEAPS + default y + source "drivers/tee/optee/Kconfig" source "drivers/tee/amdtee/Kconfig" source "drivers/tee/tstee/Kconfig"
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_core.c +4 -0
@@ -1064,6 +1064,8 @@ void tee_device_unregister(struct tee_device *teedev)if (!teedev)return ; +tee_device_put_all_dma_heaps (teedev); +if (teedev->flags &TEE_DEVICE_FLAG_REGISTERED )cdev_device_del (&teedev->cdev, &teedev->dev);
@@ -1287,3 +1289,5 @@ MODULE_AUTHOR("Linaro");MODULE_DESCRIPTION ("TEE Driver" );MODULE_VERSION ("1.0" );MODULE_LICENSE ("GPL v2" ); +MODULE_IMPORT_NS ("DMA_BUF" ); +MODULE_IMPORT_NS ("DMA_BUF_HEAP" );
drivers/tee/tee_heap.c new file +500 -0
500 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 +53 -0
86 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v12 3/9] tee: implement protected DMA-heap
drivers/tee/tee_heap.c line 242
Quoted 13 lines · Jens Wiklander
drivers/tee/tee_heap.c new file +13
-0+static const char *heap_id_2_name (enum tee_dma_heap_id id) +{ +switch (id) { +case TEE_DMA_HEAP_SECURE_VIDEO_PLAY : +return "protected,secure-video" ; +case TEE_DMA_HEAP_TRUSTED_UI : +return "protected,trusted-ui" ; +case TEE_DMA_HEAP_SECURE_VIDEO_RECORD : +return "protected,secure-video-record" ; +default : +return NULL ; + } +}
We've recently agreed on a naming guideline (even though it's not merged yet)
https://lore.kernel.org/r/[email protected]
Secure and trusted should be defined I guess, because secure and protected at least seem redundant to me.
Maxime
Re: [PATCH v12 3/9] tee: implement protected DMA-heap
Hi,
Quoted 18 lines · Maxime Ripard
drivers/tee/tee_heap.c line 242
Quoted 13 lines · Jens Wiklander
drivers/tee/tee_heap.c new file +13
-0+static const char *heap_id_2_name (enum tee_dma_heap_id id) +{ +switch (id) { +case TEE_DMA_HEAP_SECURE_VIDEO_PLAY : +return "protected,secure-video" ; +case TEE_DMA_HEAP_TRUSTED_UI : +return "protected,trusted-ui" ; +case TEE_DMA_HEAP_SECURE_VIDEO_RECORD : +return "protected,secure-video-record" ; +default : +return NULL ; + } +}We've recently agreed on a naming guideline (even though it's not merged yet)
Quoted 1 line
I wasn't aware of that (or had forgotten it), but during the revisions of this patch set, we changed to use "protected".
Quoted 3 lines
Secure and trusted should be defined I guess, because secure and protected at least seem redundant to me.
Depending on the use case, the protected buffer is only accessible to a specific set of devices. This is typically configured by the TEE firmware based on which heap we're using. To distinguish between the different heaps, I've simply added the name of the use case after the comma. So the name of the heap for the Trusted-UI use case is "protected,trusted-ui". What would a heap called "protected,ui" represent? Protected buffers for a UI use case? What kind of UI use case? If the name of the heap is too generic, it might cover more than one use case with conflicting requirements for which devices should be able to access the protected memory.
Thanks,
Jens
Re: [PATCH v12 3/9] tee: implement protected DMA-heap
Quoted 21 lines · Jens Wiklander
Hi,
Quoted 18 lines · Maxime Ripard
drivers/tee/tee_heap.c line 242
Quoted 13 lines · Jens Wiklander
drivers/tee/tee_heap.c new file +13
-0+static const char *heap_id_2_name (enum tee_dma_heap_id id) +{ +switch (id) { +case TEE_DMA_HEAP_SECURE_VIDEO_PLAY : +return "protected,secure-video" ; +case TEE_DMA_HEAP_TRUSTED_UI : +return "protected,trusted-ui" ; +case TEE_DMA_HEAP_SECURE_VIDEO_RECORD : +return "protected,secure-video-record" ; +default : +return NULL ; + } +}We've recently agreed on a naming guideline (even though it's not merged yet)
Quoted 4 lines
Quoted 1 line
I wasn't aware of that (or had forgotten it), but during the revisions of this patch set, we changed to use "protected".
I think protected is fine and what is documented in that patch, right?
Quoted 9 lines
Quoted 2 lines
Secure and trusted should be defined I guess, because secure and protected at least seem redundant to me.
Depending on the use case, the protected buffer is only accessible to a specific set of devices. This is typically configured by the TEE firmware based on which heap we're using. To distinguish between the different heaps, I've simply added the name of the use case after the comma. So the name of the heap for the Trusted-UI use case is "protected,trusted-ui".
I guess my point is that, without any prior knowledge of how that heap works, I have no idea what the "trusted-ui" use case actually is.
Quoted 2 lines
What would a heap called "protected,ui" represent? Protected buffers for a UI use case? What kind of UI use case?
I agree with all those questions. They apply equally to trusted-ui and secure-video though.
If you want to have a vendor-specific name, I guess that's fine. But you should at the very least document what all these heaps are for and the subtleties like the set of device that can access those buffers (or how to figure out that list if it's somewhat dynamic).
Maxime
PATCH v12 4/9 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
@@ -354,6 +354,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)
@@ -361,8 +410,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 ;
@@ -385,45 +434,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 v12 5/9 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 | 62 +++++++++++++++++++++++++++++++- drivers/tee/tee_private.h | 8 +++++ drivers/tee/tee_shm.c | 74 +++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 1 + include/linux/tee_drv.h | 10 ++++++ include/uapi/linux/tee.h | 31 ++++++++++++++++ 6 files changed, 182 insertions(+), 4 deletions(-)
drivers/tee/tee_core.c +61 -1
@@ -354,11 +354,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,
@@ -389,6 +427,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 ;
@@ -396,7 +454,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;
@@ -842,6 +900,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 :
drivers/tee/tee_private.h +8 -0
@@ -13,6 +13,14 @@#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 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 +71 -3
110 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
@@ -378,6 +378,37 @@ struct tee_ioctl_shm_register_data {__s32 id; }; +/** +* 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) +/** * TEE_IOC_SHM_REGISTER - Register shared memory argument *
Signature · 1 line
2.43.0
PATCH v12 6/9 tee: add tee_shm_alloc_dma_mem()
In reply to Jens WiklanderAdd tee_shm_alloc_dma_mem() to allocate DMA memory. The memory is represented by a tee_shm object using the new flag TEE_SHM_DMA_MEM to identify it as DMA memory. The allocated memory will later be lent to the TEE to be used as protected memory.
--- drivers/tee/tee_shm.c | 85 +++++++++++++++++++++++++++++++++++++++- include/linux/tee_core.h | 5 +++ 2 files changed, 88 insertions(+), 2 deletions(-)
drivers/tee/tee_shm.c +83 -2
@@ -5,6 +5,8 @@#include <linux/anon_inodes.h> #include <linux/device.h> #include <linux/dma-buf.h> +#include <linux/dma-mapping.h> +#include <linux/highmem.h> #include <linux/idr.h> #include <linux/io.h> #include <linux/mm.h>
@@ -13,9 +15,14 @@#include <linux/tee_core.h> #include <linux/uaccess.h> #include <linux/uio.h> -#include <linux/highmem.h> #include "tee_private.h" +struct tee_shm_dma_mem { +struct tee_shm shm; +dma_addr_t dma_addr; +struct page *page; +}; +static void shm_put_kernel_pages (struct page **pages,size_t page_count) {size_t n;
@@ -48,7 +55,16 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) {void *p = shm; -if (shm->flags &TEE_SHM_DMA_BUF ) { +if (shm->flags &TEE_SHM_DMA_MEM ) { +#if IS_ENABLED (CONFIG_TEE_DMABUF_HEAPS ) +struct tee_shm_dma_mem *dma_mem; + + dma_mem = container_of(shm, struct tee_shm_dma_mem, shm); + p = dma_mem; +dma_free_pages (&teedev->dev, shm->size, dma_mem->page, + dma_mem->dma_addr,DMA_BIDIRECTIONAL ); +#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);
@@ -268,6 +284,71 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size) }EXPORT_SYMBOL_GPL (tee_shm_alloc_priv_buf); +#if IS_ENABLED (CONFIG_TEE_DMABUF_HEAPS ) +/** +* tee_shm_alloc_dma_mem() - Allocate DMA memory as shared memory object +* @ctx: Context that allocates the shared memory +* @page_count: Number of pages +* +* The allocated memory is expected to be lent (made inaccessible to the +* kernel) to the TEE while it's used and returned (accessible to the +* kernel again) before it's freed. +* +* This function should normally only be used internally in the TEE +* drivers. +* +* @returns a pointer to 'struct tee_shm' +*/ +struct tee_shm *tee_shm_alloc_dma_mem (struct tee_context *ctx, +size_t page_count) +{ +struct tee_device *teedev = ctx->teedev; +struct tee_shm_dma_mem *dma_mem; +dma_addr_t dma_addr; +struct page *page; + +if (!tee_device_get (teedev)) +return ERR_PTR (-EINVAL ); + + page =dma_alloc_pages (&teedev->dev, page_count *PAGE_SIZE , + &dma_addr,DMA_BIDIRECTIONAL ,GFP_KERNEL ); +if (!page) + goto err_put_teedev; + + dma_mem =kzalloc (sizeof (*dma_mem),GFP_KERNEL ); +if (!dma_mem) + goto err_free_pages; + +refcount_set (&dma_mem->shm.refcount,1 ); + dma_mem->shm.ctx = ctx; + dma_mem->shm.paddr =page_to_phys (page); + dma_mem->dma_addr = dma_addr; + dma_mem->page = page; + dma_mem->shm.size = page_count *PAGE_SIZE ; + dma_mem->shm.flags =TEE_SHM_DMA_MEM ; + +teedev_ctx_get (ctx); + +return &dma_mem->shm; + +err_free_pages: +dma_free_pages (&teedev->dev, page_count *PAGE_SIZE , page, dma_addr, +DMA_BIDIRECTIONAL ); +err_put_teedev: +tee_device_put (teedev); + +return ERR_PTR (-ENOMEM ); +} +EXPORT_SYMBOL_GPL (tee_shm_alloc_dma_mem); +#else +struct tee_shm *tee_shm_alloc_dma_mem (struct tee_context *ctx, +size_t page_count) +{ +return ERR_PTR (-EINVAL ); +} +EXPORT_SYMBOL_GPL (tee_shm_alloc_dma_mem); +#endif + 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 +5 -0
@@ -29,6 +29,8 @@#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_DMA_MEM BIT(5)/* Memory allocated with */ +/* dma_alloc_pages() */ #define TEE_DEVICE_FLAG_REGISTERED 0x1#define TEE_MAX_DEV_NAME_LEN 32
@@ -298,6 +300,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_dma_mem (struct tee_context *ctx, +size_t page_count); + 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
PATCH v12 7/9 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/Kconfig | 5 +++ drivers/tee/optee/core.c | 7 ++++ drivers/tee/optee/optee_private.h | 2 + drivers/tee/optee/smc_abi.c | 69 ++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-)
drivers/tee/optee/Kconfig +5 -0
@@ -25,3 +25,8 @@ config OPTEE_INSECURE_LOAD_IMAGE Additional documentation on kernel security risks are at Documentation/tee/op-tee.rst. + +config OPTEE_STATIC_PROTMEM_POOL + bool + depends on HAS_IOMEM && TEE_DMABUF_HEAPS + default y
drivers/tee/optee/core.c +7 -0
@@ -56,6 +56,13 @@ int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action,return 0 ; } +int optee_set_dma_mask (struct optee *optee,u_int pa_width) +{ +u64 mask =DMA_BIT_MASK (min (64 , pa_width)); + +return dma_coerce_mask_and_coherent (&optee->teedev->dev, mask); +} +static void optee_bus_scan (struct work_struct *work) {WARN_ON (optee_enumerate_devices (PTA_CMD_GET_DEVICES_SUPP ));
drivers/tee/optee/optee_private.h +2 -0
@@ -274,6 +274,8 @@ struct optee_call_ctx {extern struct blocking_notifier_head optee_rpmb_intf_added; +int optee_set_dma_mask (struct optee *optee,u_int pa_width); +int optee_notif_init (struct optee *optee,u_int max_key);void optee_notif_uninit (struct optee *optee);int optee_notif_wait (struct optee *optee,u_int key,u32 timeout);
drivers/tee/optee/smc_abi.c +67 -2
93 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v12 8/9 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 | 146 ++++++++++++- drivers/tee/optee/optee_private.h | 13 +- drivers/tee/optee/protmem.c | 335 ++++++++++++++++++++++++++++++ 4 files changed, 492 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 +144 -2
182 lines not on this page · raw message
drivers/tee/optee/optee_private.h +12 -1
@@ -176,9 +176,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 {int (*from_msg_param)(struct optee *optee,struct tee_param *params,size_t num_params,const struct optee_msg_param *msg_params); +int (*lend_protmem)(struct optee *optee,struct tee_shm *protmem, +u32 *mem_attr,unsigned int ma_count, +u32 use_case); +int (*reclaim_protmem)(struct optee *optee,struct tee_shm *protmem); };/**
@@ -287,6 +296,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_dyn_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 +335 -0
335 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v12 9/9 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 | 78 +++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-)
drivers/tee/optee/smc_abi.c +75 -3
@@ -965,6 +965,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, +u32 *mem_attrs,unsigned int ma_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 */
@@ -1216,6 +1280,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)
@@ -1627,14 +1693,20 @@ static struct tee_protmem_pool *static_protmem_pool_init(struct optee *optee)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 =ERR_PTR (-EINVAL ); -int rc; +int rc = -EINVAL ; -if (!(optee->smc.sec_caps &OPTEE_SMC_SEC_CAP_PROTMEM )) +if (!protm && !dyn_protm)return 0 ; - pool =static_protmem_pool_init (optee); +if (protm) + pool =static_protmem_pool_init (optee); +if (dyn_protm &&IS_ERR (pool)) + pool =optee_protmem_alloc_dyn_pool (optee, heap_id);if (IS_ERR (pool))return PTR_ERR (pool);
Signature · 1 line
2.43.0
Hi,
Quoted 25 lines · Jens Wiklander
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_FDbefore 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) have been identified so far to serve as examples of what can be expected. The use-cases have 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.
Quoted 5 lines
Each use-case has its own protected memory pool since different use-cases require 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 (
dma_alloc_pages()) and made protected as needed by the TEE.
After the small update from v11, I'm picking up this and aiming for the next merge window.
Thanks,
Jens
Quoted 239 lines
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-v12
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 sets can be tested on RockPi in the same way using:
xtest --sdp-basicThe primitive tests 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-v12
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-basicThe 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 to SPMC_AT_EL=n to test without FF-A using the original SMC ABI instead. Please remember to do %make arm-tf-clean
for TF-A to be rebuilt properly using the new configuration.https://optee.readthedocs.io/en/latest/building/prerequisites.html
list dependencies required to build the above.The primitive 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,
JensChanges since V11:
* In "dma-buf: dma-heap: export declared functions":
- useEXPORT_SYMBOL_NS_GPL()
- Added TJ's R-B and Sumit's Ack
* In "tee: implement protected DMA-heap", import the namespaces "DMA_BUF" and "DMA_BUF_HEAP" as needed.Changes since V10:
* Changed the new ABIOPTEE_MSG_CMD_GET_PROTMEM_CONFIGto report a list of u32 memory attributes instead of u16 endpoints to make room for both endpoint and access permissions in each entry.
* In "tee: new ioctl to a registertee_shmfrom a dmabuf file descriptor", remove the unused path for DMA-bufs allocated by other means than the on in the TEE SS.
* In "tee: implement protected DMA-heap", handle unloading of the backend driver module implementing the heap. The heap is reference counted and also callstee_device_get()to guarantee that the module remains available while the heap is instantiated.
* In "optee: support protected memory allocation", usedma_coerce_mask_and_coherent()instead of open-coding the function.
* Added Sumit's R-B to
- "optee: smc abi: dynamic protected memory allocation"
- "optee: FF-A: dynamic protected memory allocation"
- "optee: support protected memory allocation"
- "tee: implement protected DMA-heap"
- "dma-buf: dma-heap: export declared functions"Changes since V9:
* Adding Sumit's R-B to "optee: sync secure world ABI headers"
* Update commit message as requested for "dma-buf: dma-heap: export declared functions".
* In "tee: implement protected DMA-heap":
- add the hidden config optionTEE_DMABUF_HEAPSto tell if the TEE subsystem can support DMA heaps
- add apfn_valid()to check that the passed physical address can be used by__pfn_to_page()and friends
- remove thememremap()call, the caller is should do that instead if needed
* In "tee: addtee_shm_alloc_dma_mem()" guard the calls todma_alloc_pages()anddma_free_pages()withTEE_DMABUF_HEAPSto avoid linking errors in some configurations
* In "optee: support protected memory allocation":
- add the hidden config optionOPTEE_STATIC_PROTMEM_POOLto tell if the driver can support a static protected memory pool
-optee_protmem_pool_init()is slightly refactored to make the patches that follow easier
- Calldevm_memremap()before callingtee_protmem_static_pool_alloc()Changes since V8:
* Usingdma_alloc_pages()instead ofcma_alloc()so the direct dependency on CMA can be removed together with the patches
"cma: exportcma_alloc()andcma_release()" and "dma-contiguous: exportdma_contiguous_default_area". The patch
* Renaming the patch "tee: addtee_shm_alloc_cma_phys_mem()" to "tee: addtee_shm_alloc_dma_mem()"
* Setting DMA mask for the OP-TEE TEE device based on input from the secure world instead of relying on the parent device so following patches are
removed: "tee:tee_device_alloc(): copydma_maskfrom parent device" and "optee: pass parent device totee_device_alloc()".
* Adding Sumit Garg's R-B to "tee: refactorparams_from_user()"
* In the patch "tee: implement protected DMA-heap", map the physical memory passed totee_protmem_static_pool_alloc().Changes since V7:
* Adding "dma-buf: dma-heap: export declared functions",
"cma: exportcma_alloc()andcma_release()", and "dma-contiguous: exportdma_contiguous_default_area" to export the symbols needed to keep the TEE subsystem as a load module.
* RemovingCONFIG_TEE_DMABUF_HEAPandCONFIG_TEE_CMAsince 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.
* Removing the patch "optee: account for direction while converting parameters"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 optionTEE_DMABUF_HEAPto tell if theDMABUF_HEAPSfunctions are available for the TEE subsystem
* Adding "tee: refactorparams_from_user()", broken out from the patch "tee: new ioctl to a registertee_shmfrom a dmabuf file descriptor"
* For "tee: new ioctl to a registertee_shmfrom a dmabuf file descriptor":
- Update commit message to mention protected memory
- Remove and open codetee_shm_get_parent_shm()inparam_from_user_memref()
* In "tee: addtee_shm_alloc_cma_phys_mem" add the hidden config optionTEE_CMAto tell if the CMA functions are available for the TEE subsystem
* For "tee:tee_device_alloc(): copydma_maskfrom parent device" and "optee: pass parent device totee_device_alloc", addedReviewed-by Sumit Garg <[email protected]>Changes since V5:
* Removing "tee: add restricted memory allocation" and
"tee: addTEE_IOC_RSTMEM_FD_INFO"
* Adding "tee: implement restricted DMA-heap",
"tee: new ioctl to a registertee_shmfrom a dmabuf file descriptor", "tee: addtee_shm_alloc_cma_phys_mem()",
"optee: pass parent device totee_device_alloc()", and
"tee:tee_device_alloc(): copydma_maskfrom parent device"
* The two TEE driver OPs "rstmem_alloc()" and "rstmem_free()" are replaced with a structtee_rstmem_poolabstraction.
* Replaced the theTEE_IOC_RSTMEM_ALLOCuser space API with the DMA-heap APIChanges since V4:
* Adding the patch "tee: addTEE_IOC_RSTMEM_FD_INFO" needed by the GStreamer demo
* Removing the dummy CPU access and mmap functions from thedma_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 theuse_caseand flags field in structtee_shmu32's instead of u16's
* Add more description forTEE_IOC_RSTMEM_ALLOCin the header file
* Import namespaceDMA_BUFin module tee, reported by [email protected]
* 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 #ifdefCONFIG_CMA, effectively disabling dynamic restricted memory allocate if CMA isn't configuredChanges 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 withFFA_LENDChanges since the V1 RFC:
* Based on v6.11
* Complete rewrite, replacing the restricted heap withTEE_IOC_RSTMEM_ALLOCChanges 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 applicableEtienne Carriere (1):
tee: new ioctl to a registertee_shmfrom a dmabuf file descriptorJens Wiklander (8):
optee: sync secure world ABI headers
dma-buf: dma-heap: export declared functions
tee: implement protected DMA-heap
tee: refactorparams_from_user()
tee: addtee_shm_alloc_dma_mem()
optee: support protected memory allocation
optee: FF-A: dynamic protected memory allocation
optee: smc abi: dynamic protected memory allocationdrivers/dma-buf/dma-heap.c | 4 + drivers/tee/Kconfig | 5 + drivers/tee/Makefile | 1 + drivers/tee/optee/Kconfig | 5 + drivers/tee/optee/Makefile | 1 + drivers/tee/optee/core.c | 7 + drivers/tee/optee/ffa_abi.c | 146 ++++++++- drivers/tee/optee/optee_ffa.h | 27 +- drivers/tee/optee/optee_msg.h | 84 ++++- drivers/tee/optee/optee_private.h | 15 +- drivers/tee/optee/optee_smc.h | 37 ++- drivers/tee/optee/protmem.c | 335 ++++++++++++++++++++ drivers/tee/optee/smc_abi.c | 141 ++++++++- drivers/tee/tee_core.c | 158 +++++++--- drivers/tee/tee_heap.c | 500 ++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 14 + drivers/tee/tee_shm.c | 157 +++++++++- include/linux/tee_core.h | 59 ++++ include/linux/tee_drv.h | 10 + include/uapi/linux/tee.h | 31 ++ 20 files changed, 1670 insertions(+), 67 deletions(-) create mode 100644 drivers/tee/optee/protmem.c create mode 100644 drivers/tee/tee_heap.cbase-commit:
c17b750b3ad9f45f2b6f7e6f7f4679844244f0b9--
2.43.0