Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame^] | 1 | From cb4f6a55b9c61a82a65edcd4b18c505d92480710 Mon Sep 17 00:00:00 2001 |
| 2 | From: Jens Wiklander <jens.wiklander@linaro.org> |
| 3 | Date: Thu, 25 Mar 2021 15:08:46 +0100 |
| 4 | Subject: [PATCH 15/22] optee: simplify optee_release() |
| 5 | |
| 6 | Simplifies optee_release() with a new helper function, |
| 7 | optee_close_session_helper() which has been factored out from |
| 8 | optee_close_session(). |
| 9 | |
| 10 | A separate optee_release_supp() is added for the supplicant device. |
| 11 | |
| 12 | Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org> |
| 13 | |
| 14 | Upstream-Status: Pending [Not submitted to upstream yet] |
| 15 | --- |
| 16 | drivers/tee/optee/call.c | 31 ++++++++++------- |
| 17 | drivers/tee/optee/core.c | 55 +++++++++++-------------------- |
| 18 | drivers/tee/optee/optee_private.h | 1 + |
| 19 | 3 files changed, 39 insertions(+), 48 deletions(-) |
| 20 | |
| 21 | diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c |
| 22 | index 0790de29f0ca..1b339b743ff5 100644 |
| 23 | --- a/drivers/tee/optee/call.c |
| 24 | +++ b/drivers/tee/optee/call.c |
| 25 | @@ -285,12 +285,28 @@ int optee_open_session(struct tee_context *ctx, |
| 26 | return rc; |
| 27 | } |
| 28 | |
| 29 | -int optee_close_session(struct tee_context *ctx, u32 session) |
| 30 | +int optee_close_session_helper(struct tee_context *ctx, u32 session) |
| 31 | { |
| 32 | - struct optee_context_data *ctxdata = ctx->data; |
| 33 | struct tee_shm *shm; |
| 34 | struct optee_msg_arg *msg_arg; |
| 35 | phys_addr_t msg_parg; |
| 36 | + |
| 37 | + shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg); |
| 38 | + if (IS_ERR(shm)) |
| 39 | + return PTR_ERR(shm); |
| 40 | + |
| 41 | + msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; |
| 42 | + msg_arg->session = session; |
| 43 | + optee_do_call_with_arg(ctx, msg_parg); |
| 44 | + |
| 45 | + tee_shm_free(shm); |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +int optee_close_session(struct tee_context *ctx, u32 session) |
| 51 | +{ |
| 52 | + struct optee_context_data *ctxdata = ctx->data; |
| 53 | struct optee_session *sess; |
| 54 | |
| 55 | /* Check that the session is valid and remove it from the list */ |
| 56 | @@ -303,16 +319,7 @@ int optee_close_session(struct tee_context *ctx, u32 session) |
| 57 | return -EINVAL; |
| 58 | kfree(sess); |
| 59 | |
| 60 | - shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg); |
| 61 | - if (IS_ERR(shm)) |
| 62 | - return PTR_ERR(shm); |
| 63 | - |
| 64 | - msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; |
| 65 | - msg_arg->session = session; |
| 66 | - optee_do_call_with_arg(ctx, msg_parg); |
| 67 | - |
| 68 | - tee_shm_free(shm); |
| 69 | - return 0; |
| 70 | + return optee_close_session_helper(ctx, session); |
| 71 | } |
| 72 | |
| 73 | int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, |
| 74 | diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c |
| 75 | index 63542c1cc291..e39c6d290d83 100644 |
| 76 | --- a/drivers/tee/optee/core.c |
| 77 | +++ b/drivers/tee/optee/core.c |
| 78 | @@ -263,59 +263,42 @@ static int optee_open(struct tee_context *ctx) |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | -static void optee_release(struct tee_context *ctx) |
| 83 | +static void optee_release_helper(struct tee_context *ctx, |
| 84 | + int (*close_session)(struct tee_context *ctx, |
| 85 | + u32 session)) |
| 86 | { |
| 87 | struct optee_context_data *ctxdata = ctx->data; |
| 88 | - struct tee_device *teedev = ctx->teedev; |
| 89 | - struct optee *optee = tee_get_drvdata(teedev); |
| 90 | - struct tee_shm *shm; |
| 91 | - struct optee_msg_arg *arg = NULL; |
| 92 | - phys_addr_t parg; |
| 93 | struct optee_session *sess; |
| 94 | struct optee_session *sess_tmp; |
| 95 | |
| 96 | if (!ctxdata) |
| 97 | return; |
| 98 | |
| 99 | - shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED); |
| 100 | - if (!IS_ERR(shm)) { |
| 101 | - arg = tee_shm_get_va(shm, 0); |
| 102 | - /* |
| 103 | - * If va2pa fails for some reason, we can't call into |
| 104 | - * secure world, only free the memory. Secure OS will leak |
| 105 | - * sessions and finally refuse more sessions, but we will |
| 106 | - * at least let normal world reclaim its memory. |
| 107 | - */ |
| 108 | - if (!IS_ERR(arg)) |
| 109 | - if (tee_shm_va2pa(shm, arg, &parg)) |
| 110 | - arg = NULL; /* prevent usage of parg below */ |
| 111 | - } |
| 112 | - |
| 113 | list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list, |
| 114 | list_node) { |
| 115 | list_del(&sess->list_node); |
| 116 | - if (!IS_ERR_OR_NULL(arg)) { |
| 117 | - memset(arg, 0, sizeof(*arg)); |
| 118 | - arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; |
| 119 | - arg->session = sess->session_id; |
| 120 | - optee_do_call_with_arg(ctx, parg); |
| 121 | - } |
| 122 | + close_session(ctx, sess->session_id); |
| 123 | kfree(sess); |
| 124 | } |
| 125 | kfree(ctxdata); |
| 126 | + ctx->data = NULL; |
| 127 | +} |
| 128 | |
| 129 | - if (!IS_ERR(shm)) |
| 130 | - tee_shm_free(shm); |
| 131 | +static void optee_release(struct tee_context *ctx) |
| 132 | +{ |
| 133 | + optee_release_helper(ctx, optee_close_session_helper); |
| 134 | +} |
| 135 | |
| 136 | - ctx->data = NULL; |
| 137 | +static void optee_release_supp(struct tee_context *ctx) |
| 138 | +{ |
| 139 | + struct optee *optee = tee_get_drvdata(ctx->teedev); |
| 140 | |
| 141 | - if (teedev == optee->supp_teedev) { |
| 142 | - if (optee->scan_bus_wq) { |
| 143 | - destroy_workqueue(optee->scan_bus_wq); |
| 144 | - optee->scan_bus_wq = NULL; |
| 145 | - } |
| 146 | - optee_supp_release(&optee->supp); |
| 147 | + optee_release_helper(ctx, optee_close_session_helper); |
| 148 | + if (optee->scan_bus_wq) { |
| 149 | + destroy_workqueue(optee->scan_bus_wq); |
| 150 | + optee->scan_bus_wq = NULL; |
| 151 | } |
| 152 | + optee_supp_release(&optee->supp); |
| 153 | } |
| 154 | |
| 155 | static const struct tee_driver_ops optee_ops = { |
| 156 | @@ -339,7 +322,7 @@ static const struct tee_desc optee_desc = { |
| 157 | static const struct tee_driver_ops optee_supp_ops = { |
| 158 | .get_version = optee_get_version, |
| 159 | .open = optee_open, |
| 160 | - .release = optee_release, |
| 161 | + .release = optee_release_supp, |
| 162 | .supp_recv = optee_supp_recv, |
| 163 | .supp_send = optee_supp_send, |
| 164 | .shm_register = optee_shm_register_supp, |
| 165 | diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h |
| 166 | index e25b216a14ef..2b63b796645e 100644 |
| 167 | --- a/drivers/tee/optee/optee_private.h |
| 168 | +++ b/drivers/tee/optee/optee_private.h |
| 169 | @@ -152,6 +152,7 @@ u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg); |
| 170 | int optee_open_session(struct tee_context *ctx, |
| 171 | struct tee_ioctl_open_session_arg *arg, |
| 172 | struct tee_param *param); |
| 173 | +int optee_close_session_helper(struct tee_context *ctx, u32 session); |
| 174 | int optee_close_session(struct tee_context *ctx, u32 session); |
| 175 | int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, |
| 176 | struct tee_param *param); |
| 177 | -- |
| 178 | 2.17.1 |
| 179 | |