Skip to content

Commit c3dc99d

Browse files
etienne-lmsjforissier
authored andcommitted
tee: optee: support tracking system threads
Adds support in the OP-TEE driver to keep track of reserved system threads. The logic allows one OP-TEE thread to be reserved to TEE system sessions. The optee_cq_*() functions are updated to handle this if enabled, that is when TEE describes how many thread context it supports and when at least 1 session has registered as a system session (using tee_client_system_session()). For sake of simplicity, initialization of call queue management is factorized into new helper function optee_cq_init(). The SMC ABI part of the driver enables this tracking, but the FF-A ABI part does not. Co-developed-by: Jens Wiklander <[email protected]> Co-developed-by: Sumit Garg <[email protected]> Signed-off-by: Sumit Garg <[email protected]> Signed-off-by: Etienne Carriere <[email protected]> Reviewed-by: Sumit Garg <[email protected]> Signed-off-by: Jens Wiklander <[email protected]>
1 parent dd5c899 commit c3dc99d

File tree

4 files changed

+141
-4
lines changed

4 files changed

+141
-4
lines changed

drivers/tee/optee/call.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,29 @@ struct optee_shm_arg_entry {
3939
DECLARE_BITMAP(map, MAX_ARG_COUNT_PER_ENTRY);
4040
};
4141

42+
void optee_cq_init(struct optee_call_queue *cq, int thread_count)
43+
{
44+
mutex_init(&cq->mutex);
45+
INIT_LIST_HEAD(&cq->waiters);
46+
47+
/*
48+
* If cq->total_thread_count is 0 then we're not trying to keep
49+
* track of how many free threads we have, instead we're relying on
50+
* the secure world to tell us when we're out of thread and have to
51+
* wait for another thread to become available.
52+
*/
53+
cq->total_thread_count = thread_count;
54+
cq->free_thread_count = thread_count;
55+
}
56+
4257
void optee_cq_wait_init(struct optee_call_queue *cq,
4358
struct optee_call_waiter *w, bool sys_thread)
4459
{
60+
unsigned int free_thread_threshold;
61+
bool need_wait = false;
62+
63+
memset(w, 0, sizeof(*w));
64+
4565
/*
4666
* We're preparing to make a call to secure world. In case we can't
4767
* allocate a thread in secure world we'll end up waiting in
@@ -60,8 +80,38 @@ void optee_cq_wait_init(struct optee_call_queue *cq,
6080
*/
6181
init_completion(&w->c);
6282
list_add_tail(&w->list_node, &cq->waiters);
83+
w->sys_thread = sys_thread;
84+
85+
if (cq->total_thread_count) {
86+
if (sys_thread || !cq->sys_thread_req_count)
87+
free_thread_threshold = 0;
88+
else
89+
free_thread_threshold = 1;
90+
91+
if (cq->free_thread_count > free_thread_threshold)
92+
cq->free_thread_count--;
93+
else
94+
need_wait = true;
95+
}
6396

6497
mutex_unlock(&cq->mutex);
98+
99+
while (need_wait) {
100+
optee_cq_wait_for_completion(cq, w);
101+
mutex_lock(&cq->mutex);
102+
103+
if (sys_thread || !cq->sys_thread_req_count)
104+
free_thread_threshold = 0;
105+
else
106+
free_thread_threshold = 1;
107+
108+
if (cq->free_thread_count > free_thread_threshold) {
109+
cq->free_thread_count--;
110+
need_wait = false;
111+
}
112+
113+
mutex_unlock(&cq->mutex);
114+
}
65115
}
66116

67117
void optee_cq_wait_for_completion(struct optee_call_queue *cq,
@@ -83,6 +133,14 @@ static void optee_cq_complete_one(struct optee_call_queue *cq)
83133
{
84134
struct optee_call_waiter *w;
85135

136+
/* Wake a waiting system session if any, prior to a normal session */
137+
list_for_each_entry(w, &cq->waiters, list_node) {
138+
if (w->sys_thread && !completion_done(&w->c)) {
139+
complete(&w->c);
140+
return;
141+
}
142+
}
143+
86144
list_for_each_entry(w, &cq->waiters, list_node) {
87145
if (!completion_done(&w->c)) {
88146
complete(&w->c);
@@ -104,6 +162,8 @@ void optee_cq_wait_final(struct optee_call_queue *cq,
104162
/* Get out of the list */
105163
list_del(&w->list_node);
106164

165+
cq->free_thread_count++;
166+
107167
/* Wake up one eventual waiting task */
108168
optee_cq_complete_one(cq);
109169

@@ -119,6 +179,28 @@ void optee_cq_wait_final(struct optee_call_queue *cq,
119179
mutex_unlock(&cq->mutex);
120180
}
121181

182+
/* Count registered system sessions to reserved a system thread or not */
183+
static bool optee_cq_incr_sys_thread_count(struct optee_call_queue *cq)
184+
{
185+
if (cq->total_thread_count <= 1)
186+
return false;
187+
188+
mutex_lock(&cq->mutex);
189+
cq->sys_thread_req_count++;
190+
mutex_unlock(&cq->mutex);
191+
192+
return true;
193+
}
194+
195+
static void optee_cq_decr_sys_thread_count(struct optee_call_queue *cq)
196+
{
197+
mutex_lock(&cq->mutex);
198+
cq->sys_thread_req_count--;
199+
/* If there's someone waiting, let it resume */
200+
optee_cq_complete_one(cq);
201+
mutex_unlock(&cq->mutex);
202+
}
203+
122204
/* Requires the filpstate mutex to be held */
123205
static struct optee_session *find_session(struct optee_context_data *ctxdata,
124206
u32 session_id)
@@ -361,6 +443,27 @@ int optee_open_session(struct tee_context *ctx,
361443
return rc;
362444
}
363445

446+
int optee_system_session(struct tee_context *ctx, u32 session)
447+
{
448+
struct optee *optee = tee_get_drvdata(ctx->teedev);
449+
struct optee_context_data *ctxdata = ctx->data;
450+
struct optee_session *sess;
451+
int rc = -EINVAL;
452+
453+
mutex_lock(&ctxdata->mutex);
454+
455+
sess = find_session(ctxdata, session);
456+
if (sess && (sess->use_sys_thread ||
457+
optee_cq_incr_sys_thread_count(&optee->call_queue))) {
458+
sess->use_sys_thread = true;
459+
rc = 0;
460+
}
461+
462+
mutex_unlock(&ctxdata->mutex);
463+
464+
return rc;
465+
}
466+
364467
int optee_close_session_helper(struct tee_context *ctx, u32 session,
365468
bool system_thread)
366469
{
@@ -380,6 +483,9 @@ int optee_close_session_helper(struct tee_context *ctx, u32 session,
380483

381484
optee_free_msg_arg(ctx, entry, offs);
382485

486+
if (system_thread)
487+
optee_cq_decr_sys_thread_count(&optee->call_queue);
488+
383489
return 0;
384490
}
385491

drivers/tee/optee/ffa_abi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev)
852852
if (rc)
853853
goto err_unreg_supp_teedev;
854854
mutex_init(&optee->ffa.mutex);
855-
mutex_init(&optee->call_queue.mutex);
856-
INIT_LIST_HEAD(&optee->call_queue.waiters);
855+
optee_cq_init(&optee->call_queue, 0);
857856
optee_supp_init(&optee->supp);
858857
optee_shm_arg_cache_init(optee, arg_cache_flags);
859858
ffa_dev_set_drvdata(ffa_dev, optee);

drivers/tee/optee/optee_private.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,33 @@ typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
4040
unsigned long, unsigned long,
4141
struct arm_smccc_res *);
4242

43+
/*
44+
* struct optee_call_waiter - TEE entry may need to wait for a free TEE thread
45+
* @list_node Reference in waiters list
46+
* @c Waiting completion reference
47+
* @sys_thread_req True if waiter belongs to a system thread
48+
*/
4349
struct optee_call_waiter {
4450
struct list_head list_node;
4551
struct completion c;
52+
bool sys_thread;
4653
};
4754

55+
/*
56+
* struct optee_call_queue - OP-TEE call queue management
57+
* @mutex Serializes access to this struct
58+
* @waiters List of threads waiting to enter OP-TEE
59+
* @total_thread_count Overall number of thread context in OP-TEE or 0
60+
* @free_thread_count Number of threads context free in OP-TEE
61+
* @sys_thread_req_count Number of registered system thread sessions
62+
*/
4863
struct optee_call_queue {
4964
/* Serializes access to this struct */
5065
struct mutex mutex;
5166
struct list_head waiters;
67+
int total_thread_count;
68+
int free_thread_count;
69+
int sys_thread_req_count;
5270
};
5371

5472
struct optee_notif {
@@ -230,6 +248,7 @@ int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
230248
int optee_open_session(struct tee_context *ctx,
231249
struct tee_ioctl_open_session_arg *arg,
232250
struct tee_param *param);
251+
int optee_system_session(struct tee_context *ctx, u32 session);
233252
int optee_close_session_helper(struct tee_context *ctx, u32 session,
234253
bool system_thread);
235254
int optee_close_session(struct tee_context *ctx, u32 session);
@@ -279,6 +298,7 @@ static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
279298
mp->u.value.c = p->u.value.c;
280299
}
281300

301+
void optee_cq_init(struct optee_call_queue *cq, int thread_count);
282302
void optee_cq_wait_init(struct optee_call_queue *cq,
283303
struct optee_call_waiter *w, bool sys_thread);
284304
void optee_cq_wait_for_completion(struct optee_call_queue *cq,

drivers/tee/optee/smc_abi.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ static const struct tee_driver_ops optee_clnt_ops = {
10921092
.release = optee_release,
10931093
.open_session = optee_open_session,
10941094
.close_session = optee_close_session,
1095+
.system_session = optee_system_session,
10951096
.invoke_func = optee_invoke_func,
10961097
.cancel_req = optee_cancel_req,
10971098
.shm_register = optee_shm_register,
@@ -1223,6 +1224,16 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
12231224
return true;
12241225
}
12251226

1227+
static unsigned int optee_msg_get_thread_count(optee_invoke_fn *invoke_fn)
1228+
{
1229+
struct arm_smccc_res res;
1230+
1231+
invoke_fn(OPTEE_SMC_GET_THREAD_COUNT, 0, 0, 0, 0, 0, 0, 0, &res);
1232+
if (res.a0)
1233+
return 0;
1234+
return res.a1;
1235+
}
1236+
12261237
static struct tee_shm_pool *
12271238
optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
12281239
{
@@ -1362,6 +1373,7 @@ static int optee_probe(struct platform_device *pdev)
13621373
struct optee *optee = NULL;
13631374
void *memremaped_shm = NULL;
13641375
unsigned int rpc_param_count;
1376+
unsigned int thread_count;
13651377
struct tee_device *teedev;
13661378
struct tee_context *ctx;
13671379
u32 max_notif_value;
@@ -1385,6 +1397,7 @@ static int optee_probe(struct platform_device *pdev)
13851397
return -EINVAL;
13861398
}
13871399

1400+
thread_count = optee_msg_get_thread_count(invoke_fn);
13881401
if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps,
13891402
&max_notif_value,
13901403
&rpc_param_count)) {
@@ -1474,8 +1487,7 @@ static int optee_probe(struct platform_device *pdev)
14741487
if (rc)
14751488
goto err_unreg_supp_teedev;
14761489

1477-
mutex_init(&optee->call_queue.mutex);
1478-
INIT_LIST_HEAD(&optee->call_queue.waiters);
1490+
optee_cq_init(&optee->call_queue, thread_count);
14791491
optee_supp_init(&optee->supp);
14801492
optee->smc.memremaped_shm = memremaped_shm;
14811493
optee->pool = pool;

0 commit comments

Comments
 (0)