Skip to content

Commit 0cbaf65

Browse files
qc-azarrabijenswi-linaro
authored andcommitted
tee: add close_context to TEE driver operation
The tee_context can be used to manage TEE user resources, including those allocated by the driver for the TEE on behalf of the user. The release() callback is invoked only when all resources, such as tee_shm, are released and there are no references to the tee_context. When a user closes the device file, the driver should notify the TEE to release any resources it may hold and drop the context references. To achieve this, a close_context() callback is introduced to initiate resource release in the TEE driver when the device file is closed. Relocate teedev_ctx_get, teedev_ctx_put, tee_device_get, and tee_device_get functions to tee_core.h to make them accessible outside the TEE subsystem. Reviewed-by: Sumit Garg <sumit.garg@oss.qualcomm.com> Tested-by: Neil Armstrong <neil.armstrong@linaro.org> Tested-by: Harshal Dev <quic_hdev@quicinc.com> Signed-off-by: Amirreza Zarrabi <amirreza.zarrabi@oss.qualcomm.com> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
1 parent 6dbcd5a commit 0cbaf65

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

drivers/tee/tee_core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void teedev_ctx_get(struct tee_context *ctx)
8080

8181
kref_get(&ctx->refcount);
8282
}
83+
EXPORT_SYMBOL_GPL(teedev_ctx_get);
8384

8485
static void teedev_ctx_release(struct kref *ref)
8586
{
@@ -97,11 +98,15 @@ void teedev_ctx_put(struct tee_context *ctx)
9798

9899
kref_put(&ctx->refcount, teedev_ctx_release);
99100
}
101+
EXPORT_SYMBOL_GPL(teedev_ctx_put);
100102

101103
void teedev_close_context(struct tee_context *ctx)
102104
{
103105
struct tee_device *teedev = ctx->teedev;
104106

107+
if (teedev->desc->ops->close_context)
108+
teedev->desc->ops->close_context(ctx);
109+
105110
teedev_ctx_put(ctx);
106111
tee_device_put(teedev);
107112
}
@@ -1112,6 +1117,7 @@ void tee_device_put(struct tee_device *teedev)
11121117
}
11131118
mutex_unlock(&teedev->mutex);
11141119
}
1120+
EXPORT_SYMBOL_GPL(tee_device_put);
11151121

11161122
bool tee_device_get(struct tee_device *teedev)
11171123
{
@@ -1124,6 +1130,7 @@ bool tee_device_get(struct tee_device *teedev)
11241130
mutex_unlock(&teedev->mutex);
11251131
return true;
11261132
}
1133+
EXPORT_SYMBOL_GPL(tee_device_get);
11271134

11281135
/**
11291136
* tee_device_unregister() - Removes a TEE device

drivers/tee/tee_private.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ struct tee_shm_dmabuf_ref {
2323

2424
int tee_shm_get_fd(struct tee_shm *shm);
2525

26-
bool tee_device_get(struct tee_device *teedev);
27-
void tee_device_put(struct tee_device *teedev);
28-
29-
void teedev_ctx_get(struct tee_context *ctx);
30-
void teedev_ctx_put(struct tee_context *ctx);
31-
3226
struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size);
3327
struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
3428
unsigned long addr, size_t length);

include/linux/tee_core.h

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ struct tee_device {
7676
/**
7777
* struct tee_driver_ops - driver operations vtable
7878
* @get_version: returns version of driver
79-
* @open: called when the device file is opened
80-
* @release: release this open file
79+
* @open: called for a context when the device file is opened
80+
* @close_context: called when the device file is closed
81+
* @release: called to release the context
8182
* @open_session: open a new session
8283
* @close_session: close a session
8384
* @system_session: declare session as a system session
@@ -87,11 +88,17 @@ struct tee_device {
8788
* @supp_send: called for supplicant to send a response
8889
* @shm_register: register shared memory buffer in TEE
8990
* @shm_unregister: unregister shared memory buffer in TEE
91+
*
92+
* The context given to @open might last longer than the device file if it is
93+
* tied to other resources in the TEE driver. @close_context is called when the
94+
* client closes the device file, even if there are existing references to the
95+
* context. The TEE driver can use @close_context to start cleaning up.
9096
*/
9197
struct tee_driver_ops {
9298
void (*get_version)(struct tee_device *teedev,
9399
struct tee_ioctl_version_data *vers);
94100
int (*open)(struct tee_context *ctx);
101+
void (*close_context)(struct tee_context *ctx);
95102
void (*release)(struct tee_context *ctx);
96103
int (*open_session)(struct tee_context *ctx,
97104
struct tee_ioctl_open_session_arg *arg,
@@ -200,6 +207,24 @@ int tee_device_register_dma_heap(struct tee_device *teedev,
200207
struct tee_protmem_pool *pool);
201208
void tee_device_put_all_dma_heaps(struct tee_device *teedev);
202209

210+
/**
211+
* tee_device_get() - Increment the user count for a tee_device
212+
* @teedev: Pointer to the tee_device
213+
*
214+
* If tee_device_unregister() has been called and the final user of @teedev
215+
* has already released the device, this function will fail to prevent new users
216+
* from accessing the device during the unregistration process.
217+
*
218+
* Returns: true if @teedev remains valid, otherwise false
219+
*/
220+
bool tee_device_get(struct tee_device *teedev);
221+
222+
/**
223+
* tee_device_put() - Decrease the user count for a tee_device
224+
* @teedev: pointer to the tee_device
225+
*/
226+
void tee_device_put(struct tee_device *teedev);
227+
203228
/**
204229
* tee_device_set_dev_groups() - Set device attribute groups
205230
* @teedev: Device to register
@@ -374,4 +399,25 @@ struct tee_context *teedev_open(struct tee_device *teedev);
374399
*/
375400
void teedev_close_context(struct tee_context *ctx);
376401

402+
/**
403+
* teedev_ctx_get() - Increment the reference count of a context
404+
* @ctx: Pointer to the context
405+
*
406+
* This function increases the refcount of the context, which is tied to
407+
* resources shared by the same tee_device. During the unregistration process,
408+
* the context may remain valid even after tee_device_unregister() has returned.
409+
*
410+
* Users should ensure that the context's refcount is properly decreased before
411+
* calling tee_device_put(), typically within the context's release() function.
412+
* Alternatively, users can call tee_device_get() and teedev_ctx_get() together
413+
* and release them simultaneously (see shm_alloc_helper()).
414+
*/
415+
void teedev_ctx_get(struct tee_context *ctx);
416+
417+
/**
418+
* teedev_ctx_put() - Decrease reference count on a context
419+
* @ctx: pointer to the context
420+
*/
421+
void teedev_ctx_put(struct tee_context *ctx);
422+
377423
#endif /*__TEE_CORE_H*/

0 commit comments

Comments
 (0)