forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Another attempt at Provision TEE threads for system invocation #110
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a6ef82f
tee: optee: system call property
etienne-lms 1bbea8d
tee: system invocation
jenswi-linaro 3fdc853
tee: optee: support tracking system threads
jenswi-linaro 7ad5e02
firmware: arm_scmi: optee: use optee system invocation
etienne-lms 1b372e0
[review] tee: optee: support tracking system threads
jenswi-linaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,9 +39,26 @@ struct optee_shm_arg_entry { | |
DECLARE_BITMAP(map, MAX_ARG_COUNT_PER_ENTRY); | ||
}; | ||
|
||
void optee_cq_init(struct optee_call_queue *cq, int thread_count) | ||
{ | ||
mutex_init(&cq->mutex); | ||
INIT_LIST_HEAD(&cq->normal_waiters); | ||
INIT_LIST_HEAD(&cq->sys_waiters); | ||
/* | ||
* If cq->total_thread_count is 0 then we're not trying to keep | ||
* track of how many free threads we have, instead we're relying on | ||
* the secure world to tell us when we're out of thread and have to | ||
* wait for another thread to become available. | ||
*/ | ||
cq->total_thread_count = thread_count; | ||
cq->free_normal_thread_count = thread_count; | ||
} | ||
|
||
void optee_cq_wait_init(struct optee_call_queue *cq, | ||
struct optee_call_waiter *w) | ||
struct optee_call_waiter *w, bool sys_thread) | ||
{ | ||
bool need_wait = false; | ||
|
||
/* | ||
* We're preparing to make a call to secure world. In case we can't | ||
* allocate a thread in secure world we'll end up waiting in | ||
|
@@ -53,15 +70,40 @@ void optee_cq_wait_init(struct optee_call_queue *cq, | |
mutex_lock(&cq->mutex); | ||
|
||
/* | ||
* We add ourselves to the queue, but we don't wait. This | ||
* guarantees that we don't lose a completion if secure world | ||
* returns busy and another thread just exited and try to complete | ||
* someone. | ||
* We add ourselves to a queue, but we don't wait. This guarantees | ||
* that we don't lose a completion if secure world returns busy and | ||
* another thread just exited and try to complete someone. | ||
*/ | ||
init_completion(&w->c); | ||
list_add_tail(&w->list_node, &cq->waiters); | ||
w->sys_thread = sys_thread; | ||
if (sys_thread) { | ||
list_add_tail(&w->list_node, &cq->sys_waiters); | ||
} else { | ||
list_add_tail(&w->list_node, &cq->normal_waiters); | ||
if (cq->total_thread_count) { | ||
/* | ||
* Claim a normal thread if one is available, else | ||
* we'll need to wait for a normal thread to be | ||
* released. | ||
*/ | ||
if (cq->free_normal_thread_count > 0) | ||
cq->free_normal_thread_count--; | ||
else | ||
need_wait = true; | ||
} | ||
} | ||
|
||
mutex_unlock(&cq->mutex); | ||
|
||
while (need_wait) { | ||
optee_cq_wait_for_completion(cq, w); | ||
mutex_lock(&cq->mutex); | ||
if (cq->free_normal_thread_count > 0) { | ||
cq->free_normal_thread_count--; | ||
need_wait = false; | ||
} | ||
mutex_unlock(&cq->mutex); | ||
} | ||
} | ||
|
||
void optee_cq_wait_for_completion(struct optee_call_queue *cq, | ||
|
@@ -74,7 +116,10 @@ void optee_cq_wait_for_completion(struct optee_call_queue *cq, | |
/* Move to end of list to get out of the way for other waiters */ | ||
list_del(&w->list_node); | ||
reinit_completion(&w->c); | ||
list_add_tail(&w->list_node, &cq->waiters); | ||
if (w->sys_thread) | ||
list_add_tail(&w->list_node, &cq->sys_waiters); | ||
else | ||
list_add_tail(&w->list_node, &cq->normal_waiters); | ||
|
||
mutex_unlock(&cq->mutex); | ||
} | ||
|
@@ -83,10 +128,19 @@ static void optee_cq_complete_one(struct optee_call_queue *cq) | |
{ | ||
struct optee_call_waiter *w; | ||
|
||
list_for_each_entry(w, &cq->waiters, list_node) { | ||
list_for_each_entry(w, &cq->sys_waiters, list_node) { | ||
if (!completion_done(&w->c)) { | ||
complete(&w->c); | ||
break; | ||
return; | ||
} | ||
} | ||
|
||
if (!cq->total_thread_count || cq->free_normal_thread_count > 0) { | ||
list_for_each_entry(w, &cq->normal_waiters, list_node) { | ||
if (!completion_done(&w->c)) { | ||
complete(&w->c); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -104,6 +158,9 @@ void optee_cq_wait_final(struct optee_call_queue *cq, | |
/* Get out of the list */ | ||
list_del(&w->list_node); | ||
|
||
if (!w->sys_thread) | ||
cq->free_normal_thread_count++; /* Release a normal thread */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mutex? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're holding |
||
|
||
/* Wake up one eventual waiting task */ | ||
optee_cq_complete_one(cq); | ||
|
||
|
@@ -119,6 +176,36 @@ void optee_cq_wait_final(struct optee_call_queue *cq, | |
mutex_unlock(&cq->mutex); | ||
} | ||
|
||
bool optee_cq_inc_sys_thread_count(struct optee_call_queue *cq) | ||
{ | ||
bool rc = false; | ||
|
||
mutex_lock(&cq->mutex); | ||
|
||
/* Leave at least 1 normal (non-system) thread */ | ||
if (cq->res_sys_thread_count + 1 < cq->total_thread_count) { | ||
cq->free_normal_thread_count--; | ||
cq->res_sys_thread_count++; | ||
rc = true; | ||
} | ||
|
||
mutex_unlock(&cq->mutex); | ||
|
||
return rc; | ||
} | ||
|
||
void optee_cq_dec_sys_thread_count(struct optee_call_queue *cq) | ||
{ | ||
mutex_lock(&cq->mutex); | ||
if (cq->res_sys_thread_count > 0) { | ||
cq->res_sys_thread_count--; | ||
cq->free_normal_thread_count++; | ||
/* If there's someone waiting, let it resume */ | ||
optee_cq_complete_one(cq); | ||
} | ||
mutex_unlock(&cq->mutex); | ||
} | ||
|
||
/* Requires the filpstate mutex to be held */ | ||
static struct optee_session *find_session(struct optee_context_data *ctxdata, | ||
u32 session_id) | ||
|
@@ -328,7 +415,8 @@ int optee_open_session(struct tee_context *ctx, | |
goto out; | ||
} | ||
|
||
if (optee->ops->do_call_with_arg(ctx, shm, offs)) { | ||
if (optee->ops->do_call_with_arg(ctx, shm, offs, | ||
sess->use_sys_thread)) { | ||
msg_arg->ret = TEEC_ERROR_COMMUNICATION; | ||
msg_arg->ret_origin = TEEC_ORIGIN_COMMS; | ||
} | ||
|
@@ -360,7 +448,29 @@ int optee_open_session(struct tee_context *ctx, | |
return rc; | ||
} | ||
|
||
int optee_close_session_helper(struct tee_context *ctx, u32 session) | ||
int optee_system_session(struct tee_context *ctx, u32 session) | ||
{ | ||
struct optee_context_data *ctxdata = ctx->data; | ||
struct optee *optee = tee_get_drvdata(ctx->teedev); | ||
struct optee_session *sess; | ||
int rc = -EINVAL; | ||
|
||
mutex_lock(&ctxdata->mutex); | ||
|
||
sess = find_session(ctxdata, session); | ||
if (sess && !sess->use_sys_thread && | ||
optee_cq_inc_sys_thread_count(&optee->call_queue)) { | ||
rc = 0; | ||
sess->use_sys_thread = true; | ||
} | ||
|
||
mutex_unlock(&ctxdata->mutex); | ||
|
||
return rc; | ||
} | ||
|
||
int optee_close_session_helper(struct tee_context *ctx, u32 session, | ||
bool system_thread) | ||
{ | ||
struct optee *optee = tee_get_drvdata(ctx->teedev); | ||
struct optee_shm_arg_entry *entry; | ||
|
@@ -374,8 +484,10 @@ int optee_close_session_helper(struct tee_context *ctx, u32 session) | |
|
||
msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; | ||
msg_arg->session = session; | ||
optee->ops->do_call_with_arg(ctx, shm, offs); | ||
optee->ops->do_call_with_arg(ctx, shm, offs, system_thread); | ||
|
||
if (system_thread) | ||
optee_cq_dec_sys_thread_count(&optee->call_queue); | ||
optee_free_msg_arg(ctx, entry, offs); | ||
|
||
return 0; | ||
|
@@ -385,6 +497,7 @@ int optee_close_session(struct tee_context *ctx, u32 session) | |
{ | ||
struct optee_context_data *ctxdata = ctx->data; | ||
struct optee_session *sess; | ||
bool system_thread; | ||
|
||
/* Check that the session is valid and remove it from the list */ | ||
mutex_lock(&ctxdata->mutex); | ||
|
@@ -394,9 +507,10 @@ int optee_close_session(struct tee_context *ctx, u32 session) | |
mutex_unlock(&ctxdata->mutex); | ||
if (!sess) | ||
return -EINVAL; | ||
system_thread = sess->use_sys_thread; | ||
kfree(sess); | ||
|
||
return optee_close_session_helper(ctx, session); | ||
return optee_close_session_helper(ctx, session, system_thread); | ||
} | ||
|
||
int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, | ||
|
@@ -408,12 +522,15 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, | |
struct optee_msg_arg *msg_arg; | ||
struct optee_session *sess; | ||
struct tee_shm *shm; | ||
bool system_thread; | ||
u_int offs; | ||
int rc; | ||
|
||
/* Check that the session is valid */ | ||
mutex_lock(&ctxdata->mutex); | ||
sess = find_session(ctxdata, arg->session); | ||
if (sess) | ||
system_thread = sess->use_sys_thread; | ||
mutex_unlock(&ctxdata->mutex); | ||
if (!sess) | ||
return -EINVAL; | ||
|
@@ -432,7 +549,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, | |
if (rc) | ||
goto out; | ||
|
||
if (optee->ops->do_call_with_arg(ctx, shm, offs)) { | ||
if (optee->ops->do_call_with_arg(ctx, shm, offs, system_thread)) { | ||
msg_arg->ret = TEEC_ERROR_COMMUNICATION; | ||
msg_arg->ret_origin = TEEC_ORIGIN_COMMS; | ||
} | ||
|
@@ -457,12 +574,15 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session) | |
struct optee_shm_arg_entry *entry; | ||
struct optee_msg_arg *msg_arg; | ||
struct optee_session *sess; | ||
bool system_thread; | ||
struct tee_shm *shm; | ||
u_int offs; | ||
|
||
/* Check that the session is valid */ | ||
mutex_lock(&ctxdata->mutex); | ||
sess = find_session(ctxdata, session); | ||
if (sess) | ||
system_thread = sess->use_sys_thread; | ||
mutex_unlock(&ctxdata->mutex); | ||
if (!sess) | ||
return -EINVAL; | ||
|
@@ -474,7 +594,7 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session) | |
msg_arg->cmd = OPTEE_MSG_CMD_CANCEL; | ||
msg_arg->session = session; | ||
msg_arg->cancel_id = cancel_id; | ||
optee->ops->do_call_with_arg(ctx, shm, offs); | ||
optee->ops->do_call_with_arg(ctx, shm, offs, system_thread); | ||
|
||
optee_free_msg_arg(ctx, entry, offs); | ||
return 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some thread protection around this incrementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're holding
cq->mutex
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, sorry.