Skip to content

Commit c0ab6db

Browse files
committed
optee: simplify optee_release()
Simplifies optee_release() with a new helper function, optee_close_session_helper() which has been factored out from optee_close_session(). A separate optee_release_supp() is added for the supplicant device. Reviewed-by: Sumit Garg <[email protected]> Signed-off-by: Jens Wiklander <[email protected]>
1 parent 9028b24 commit c0ab6db

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

drivers/tee/optee/call.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,28 @@ int optee_open_session(struct tee_context *ctx,
288288
return rc;
289289
}
290290

291-
int optee_close_session(struct tee_context *ctx, u32 session)
291+
int optee_close_session_helper(struct tee_context *ctx, u32 session)
292292
{
293-
struct optee_context_data *ctxdata = ctx->data;
294293
struct tee_shm *shm;
295294
struct optee_msg_arg *msg_arg;
296295
phys_addr_t msg_parg;
296+
297+
shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
298+
if (IS_ERR(shm))
299+
return PTR_ERR(shm);
300+
301+
msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
302+
msg_arg->session = session;
303+
optee_do_call_with_arg(ctx, msg_parg);
304+
305+
tee_shm_free(shm);
306+
307+
return 0;
308+
}
309+
310+
int optee_close_session(struct tee_context *ctx, u32 session)
311+
{
312+
struct optee_context_data *ctxdata = ctx->data;
297313
struct optee_session *sess;
298314

299315
/* Check that the session is valid and remove it from the list */
@@ -306,16 +322,7 @@ int optee_close_session(struct tee_context *ctx, u32 session)
306322
return -EINVAL;
307323
kfree(sess);
308324

309-
shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
310-
if (IS_ERR(shm))
311-
return PTR_ERR(shm);
312-
313-
msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
314-
msg_arg->session = session;
315-
optee_do_call_with_arg(ctx, msg_parg);
316-
317-
tee_shm_free(shm);
318-
return 0;
325+
return optee_close_session_helper(ctx, session);
319326
}
320327

321328
int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,

drivers/tee/optee/core.c

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -264,60 +264,42 @@ static int optee_open(struct tee_context *ctx)
264264
return 0;
265265
}
266266

267-
static void optee_release(struct tee_context *ctx)
267+
static void optee_release_helper(struct tee_context *ctx,
268+
int (*close_session)(struct tee_context *ctx,
269+
u32 session))
268270
{
269271
struct optee_context_data *ctxdata = ctx->data;
270-
struct tee_device *teedev = ctx->teedev;
271-
struct optee *optee = tee_get_drvdata(teedev);
272-
struct tee_shm *shm;
273-
struct optee_msg_arg *arg = NULL;
274-
phys_addr_t parg;
275272
struct optee_session *sess;
276273
struct optee_session *sess_tmp;
277274

278275
if (!ctxdata)
279276
return;
280277

281-
shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg),
282-
TEE_SHM_MAPPED | TEE_SHM_PRIV);
283-
if (!IS_ERR(shm)) {
284-
arg = tee_shm_get_va(shm, 0);
285-
/*
286-
* If va2pa fails for some reason, we can't call into
287-
* secure world, only free the memory. Secure OS will leak
288-
* sessions and finally refuse more sessions, but we will
289-
* at least let normal world reclaim its memory.
290-
*/
291-
if (!IS_ERR(arg))
292-
if (tee_shm_va2pa(shm, arg, &parg))
293-
arg = NULL; /* prevent usage of parg below */
294-
}
295-
296278
list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
297279
list_node) {
298280
list_del(&sess->list_node);
299-
if (!IS_ERR_OR_NULL(arg)) {
300-
memset(arg, 0, sizeof(*arg));
301-
arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
302-
arg->session = sess->session_id;
303-
optee_do_call_with_arg(ctx, parg);
304-
}
281+
close_session(ctx, sess->session_id);
305282
kfree(sess);
306283
}
307284
kfree(ctxdata);
285+
ctx->data = NULL;
286+
}
308287

309-
if (!IS_ERR(shm))
310-
tee_shm_free(shm);
288+
static void optee_release(struct tee_context *ctx)
289+
{
290+
optee_release_helper(ctx, optee_close_session_helper);
291+
}
311292

312-
ctx->data = NULL;
293+
static void optee_release_supp(struct tee_context *ctx)
294+
{
295+
struct optee *optee = tee_get_drvdata(ctx->teedev);
313296

314-
if (teedev == optee->supp_teedev) {
315-
if (optee->scan_bus_wq) {
316-
destroy_workqueue(optee->scan_bus_wq);
317-
optee->scan_bus_wq = NULL;
318-
}
319-
optee_supp_release(&optee->supp);
297+
optee_release_helper(ctx, optee_close_session_helper);
298+
if (optee->scan_bus_wq) {
299+
destroy_workqueue(optee->scan_bus_wq);
300+
optee->scan_bus_wq = NULL;
320301
}
302+
optee_supp_release(&optee->supp);
321303
}
322304

323305
static const struct tee_driver_ops optee_ops = {
@@ -341,7 +323,7 @@ static const struct tee_desc optee_desc = {
341323
static const struct tee_driver_ops optee_supp_ops = {
342324
.get_version = optee_get_version,
343325
.open = optee_open,
344-
.release = optee_release,
326+
.release = optee_release_supp,
345327
.supp_recv = optee_supp_recv,
346328
.supp_send = optee_supp_send,
347329
.shm_register = optee_shm_register_supp,

drivers/tee/optee/optee_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
152152
int optee_open_session(struct tee_context *ctx,
153153
struct tee_ioctl_open_session_arg *arg,
154154
struct tee_param *param);
155+
int optee_close_session_helper(struct tee_context *ctx, u32 session);
155156
int optee_close_session(struct tee_context *ctx, u32 session);
156157
int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
157158
struct tee_param *param);

0 commit comments

Comments
 (0)