Skip to contentSkip to conversation

optee: support wq_sleep_timeout

PATCH In review gavin.liu 3 participants 5 messages Jan 25, 2024 – Jan 29, 2024

Subsystems (3)
lore
Share & tools
lore

gavin.liu

author
#0 lore raw

PATCH 1/1 optee: support wq_sleep_timeout

From: Gavin Liu <[email protected]>

Add wq_sleep_timeout to support self waking when timeout for secure driver usage.

---
 drivers/tee/optee/notif.c         |  9 +++++++--
 drivers/tee/optee/optee_private.h |  2 +-
 drivers/tee/optee/rpc.c           | 10 ++++++++--
 3 files changed, 16 insertions(+), 5 deletions(-)
drivers/tee/optee/notif.c +7 -2
index 05212842b0a5..d5e5c0645609 100644
@@ -29,7 +29,7 @@ static bool have_key(struct optee *optee, u_int key)
 	return false;
 }
 
-int optee_notif_wait(struct optee *optee, u_int key)
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout)
 {
 	unsigned long flags;
 	struct notif_entry *entry;
@@ -70,7 +70,12 @@ int optee_notif_wait(struct optee *optee, u_int key)
 	 * Unlock temporarily and wait for completion.
 	 */
 	spin_unlock_irqrestore(&optee->notif.lock, flags);
-	wait_for_completion(&entry->c);
+	if (timeout != 0) {
+		if (!wait_for_completion_timeout(&entry->c, timeout))
+			rc = -ETIMEDOUT;
+	} else {
+		wait_for_completion(&entry->c);
+	}
 	spin_lock_irqsave(&optee->notif.lock, flags);
 
 	list_del(&entry->link);
drivers/tee/optee/optee_private.h +1 -1
index 7a5243c78b55..da990c4016ec 100644
@@ -252,7 +252,7 @@ struct optee_call_ctx {
 
 int optee_notif_init(struct optee *optee, u_int max_key);
 void optee_notif_uninit(struct optee *optee);
-int optee_notif_wait(struct optee *optee, u_int key);
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);
 int optee_notif_send(struct optee *optee, u_int key);
 
 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
drivers/tee/optee/rpc.c +8 -2
index e69bc6380683..14e6246aaf05 100644
@@ -130,6 +130,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
 static void handle_rpc_func_cmd_wq(struct optee *optee,
 				   struct optee_msg_arg *arg)
 {
+	int rc = 0;
+
 	if (arg->num_params != 1)
 		goto bad;
 
@@ -139,7 +141,8 @@ static void handle_rpc_func_cmd_wq(struct optee *optee,
 
 	switch (arg->params[0].u.value.a) {
 	case OPTEE_RPC_NOTIFICATION_WAIT:
-		if (optee_notif_wait(optee, arg->params[0].u.value.b))
+		rc = optee_notif_wait(optee, arg->params[0].u.value.b, arg->params[0].u.value.c);
+		if (rc)
 			goto bad;
 		break;
 	case OPTEE_RPC_NOTIFICATION_SEND:
@@ -153,7 +156,10 @@ static void handle_rpc_func_cmd_wq(struct optee *optee,
 	arg->ret = TEEC_SUCCESS;
 	return;
 bad:
-	arg->ret = TEEC_ERROR_BAD_PARAMETERS;
+	if (rc == -ETIMEDOUT)
+		arg->ret = TEEC_ERROR_BUSY;
+	else
+		arg->ret = TEEC_ERROR_BAD_PARAMETERS;
 }
 
 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
Signature · 1 line
2.18.0
#1 lore raw
Quoted 66 lines · gavin.liu via OP-TEE

From: Gavin Liu <[email protected]>

Add wq_sleep_timeout to support self waking when timeout for secure driver usage.

---
 drivers/tee/optee/notif.c         |  9 +++++++--
 drivers/tee/optee/optee_private.h |  2 +-
 drivers/tee/optee/rpc.c           | 10 ++++++++--
 3 files changed, 16 insertions(+), 5 deletions(-)
drivers/tee/optee/notif.c +7 -2
index 05212842b0a5..d5e5c0645609 100644
@@ -29,7 +29,7 @@ static bool have_key(struct optee *optee, u_int key)
 	return false;
 }
 
-int optee_notif_wait(struct optee *optee, u_int key)
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout)
 {
 	unsigned long flags;
 	struct notif_entry *entry;
@@ -70,7 +70,12 @@ int optee_notif_wait(struct optee *optee, u_int key)
 	 * Unlock temporarily and wait for completion.
 	 */
 	spin_unlock_irqrestore(&optee->notif.lock, flags);
-	wait_for_completion(&entry->c);
+	if (timeout != 0) {
+		if (!wait_for_completion_timeout(&entry->c, timeout))
+			rc = -ETIMEDOUT;
+	} else {
+		wait_for_completion(&entry->c);
+	}
 	spin_lock_irqsave(&optee->notif.lock, flags);
 
 	list_del(&entry->link);
drivers/tee/optee/optee_private.h +1 -1
index 7a5243c78b55..da990c4016ec 100644
@@ -252,7 +252,7 @@ struct optee_call_ctx {
 
 int optee_notif_init(struct optee *optee, u_int max_key);
 void optee_notif_uninit(struct optee *optee);
-int optee_notif_wait(struct optee *optee, u_int key);
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);
 int optee_notif_send(struct optee *optee, u_int key);
 
 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
drivers/tee/optee/rpc.c +2 -0
index e69bc6380683..14e6246aaf05 100644
@@ -130,6 +130,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
 static void handle_rpc_func_cmd_wq(struct optee *optee,
 				   struct optee_msg_arg *arg)
 {
+	int rc = 0;
+
 	if (arg->num_params != 1)
 		goto bad;
 

drivers/tee/optee/rpc.c static void handle_rpc_func_cmd_wq(struct optee *optee, · line 144

Quoted 5 lines
drivers/tee/optee/rpc.c +1 -1
@@ -139,7 +141,8 @@ static void handle_rpc_func_cmd_wq(struct optee *optee,
 
 	switch (arg->params[0].u.value.a) {
 	case OPTEE_RPC_NOTIFICATION_WAIT:
-		if (optee_notif_wait(optee, arg->params[0].u.value.b))
+		rc = optee_notif_wait(optee, arg->params[0].u.value.b, arg->params[0].u.value.c);

optee/optee_rpc_cmd.h needs updating (near "Waiting on notification") to reflect the meaning of value.c.

Was value.c required to be zero prior to this change? Otherwise this could lead to undefined behavior.

Quoted 16 lines
drivers/tee/optee/rpc.c +5 -1
+		if (rc)
 			goto bad;
 		break;
 	case OPTEE_RPC_NOTIFICATION_SEND:
@@ -153,7 +156,10 @@ static void handle_rpc_func_cmd_wq(struct optee *optee,
 	arg->ret = TEEC_SUCCESS;
 	return;
 bad:
-	arg->ret = TEEC_ERROR_BAD_PARAMETERS;
+	if (rc == -ETIMEDOUT)
+		arg->ret = TEEC_ERROR_BUSY;
+	else
+		arg->ret = TEEC_ERROR_BAD_PARAMETERS;
 }
 
 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
Signature · 1 line
Jerome
#2 lore raw

Hi Jerome,

Thanks very much for the reviewing.

Quoted 79 lines · Jerome Forissier

External email : Please do not click links or open attachments until you have verified the sender or the content.

drivers/tee/optee/notif.c static bool have_key(struct optee *optee, u_int

Quoted 17 lines · gavin.liu via OP-TEE

From: Gavin Liu <[email protected]>

Add wq_sleep_timeout to support self waking when timeout for secure driver usage.

---
 drivers/tee/optee/notif.c         |  9 +++++++--
 drivers/tee/optee/optee_private.h |  2 +-
 drivers/tee/optee/rpc.c           | 10 ++++++++--
 3 files changed, 16 insertions(+), 5 deletions(-)
drivers/tee/optee/notif.c +0 -0
index 05212842b0a5..d5e5c0645609 100644
@@ -29,7 +29,7 @@ static bool have_key(struct optee *optee, u_int

key)

drivers/tee/optee/notif.c int optee_notif_wait(struct optee *optee, u_int

Quoted 9 lines
drivers/tee/optee/notif.c +1 -1
 return false;
 }
 
-int optee_notif_wait(struct optee *optee, u_int key)
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout)
 {
 unsigned long flags;
 struct notif_entry *entry;
@@ -70,7 +70,12 @@ int optee_notif_wait(struct optee *optee, u_int

key)

Review

Quoted 14 lines
drivers/tee/optee/notif.c +6 -1
  * Unlock temporarily and wait for completion.
  */
 spin_unlock_irqrestore(&optee->notif.lock, flags);
-wait_for_completion(&entry->c);
+if (timeout != 0) {
+if (!wait_for_completion_timeout(&entry->c, timeout))
+rc = -ETIMEDOUT;
+} else {
+wait_for_completion(&entry->c);
+}
 spin_lock_irqsave(&optee->notif.lock, flags);
 
 list_del(&entry->link);
(quoted diff) +0 -0

b/drivers/tee/optee/optee_private.h

drivers/tee/optee/optee_private.h struct optee_call_ctx { · line 258

Quoted 12 lines

index 7a5243c78b55..da990c4016ec 100644

drivers/tee/optee/optee_private.h +1 -1
@@ -252,7 +252,7 @@ struct optee_call_ctx {
 
 int optee_notif_init(struct optee *optee, u_int max_key);
 void optee_notif_uninit(struct optee *optee);
-int optee_notif_wait(struct optee *optee, u_int key);
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);
 int optee_notif_send(struct optee *optee, u_int key);
 
 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t

num_params,

drivers/tee/optee/rpc.c static void

Quoted 5 lines
drivers/tee/optee/rpc.c +0 -0
index e69bc6380683..14e6246aaf05 100644
@@ -130,6 +130,8 @@ static void

handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,

drivers/tee/optee/rpc.c static void handle_rpc_func_cmd_wq(struct optee

Quoted 9 lines
drivers/tee/optee/rpc.c +2 -0
 static void handle_rpc_func_cmd_wq(struct optee *optee,
    struct optee_msg_arg *arg)
 {
+int rc = 0;
+
 if (arg->num_params != 1)
 goto bad;
 
@@ -139,7 +141,8 @@ static void handle_rpc_func_cmd_wq(struct optee

*optee,

Quoted 1 line
Quoted 10 lines
Quoted 5 lines

switch (arg->params[0].u.value.a) {
case OPTEE_RPC_NOTIFICATION_WAIT:

drivers/tee/optee/rpc.c +1 -1
-if (optee_notif_wait(optee, arg->params[0].u.value.b))
+rc = optee_notif_wait(optee, arg->params[0].u.value.b, arg-

params[0].u.value.c);

optee/optee_rpc_cmd.h needs updating (near "Waiting on notification") to reflect the meaning
of value.c.

Ok, I will update the patch for this.

Quoted 5 lines

Was value.c required to be zero prior to this change? Otherwise this could lead to undefined
behavior.

This value comes from optee-os, and it is zero by default now.

Quoted 20 lines

drivers/tee/optee/rpc.c static void handle_rpc_func_cmd_wq(struct

Quoted 5 lines
drivers/tee/optee/rpc.c +1 -0
+if (rc)
 goto bad;
 break;
 case OPTEE_RPC_NOTIFICATION_SEND:
@@ -153,7 +156,10 @@ static void handle_rpc_func_cmd_wq(struct

optee *optee,

Quoted 11 lines
drivers/tee/optee/rpc.c +4 -1
 arg->ret = TEEC_SUCCESS;
 return;
 bad:
-arg->ret = TEEC_ERROR_BAD_PARAMETERS;
+if (rc == -ETIMEDOUT)
+arg->ret = TEEC_ERROR_BUSY;
+else
+arg->ret = TEEC_ERROR_BAD_PARAMETERS;
 }
 
 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
Signature · 1 line
Jerome
#3 lore raw
In reply to gavin.liu
Quoted 6 lines · gavin.liu

From: Gavin Liu <[email protected]>

Add wq_sleep_timeout to support self waking when timeout for secure driver usage.

Quoted 6 lines
---
 drivers/tee/optee/notif.c         |  9 +++++++--
 drivers/tee/optee/optee_private.h |  2 +-
 drivers/tee/optee/rpc.c           | 10 ++++++++--
 3 files changed, 16 insertions(+), 5 deletions(-)

I'd like to see the corresponding secure world changes taking advantage of this ABI change before we take this any further.

Thanks,
Jens

Quoted 79 lines
drivers/tee/optee/notif.c +7 -2
index 05212842b0a5..d5e5c0645609 100644
@@ -29,7 +29,7 @@ static bool have_key(struct optee *optee, u_int key)
        return false;
 }
 
-int optee_notif_wait(struct optee *optee, u_int key)
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout)
 {
        unsigned long flags;
        struct notif_entry *entry;
@@ -70,7 +70,12 @@ int optee_notif_wait(struct optee *optee, u_int key)
         * Unlock temporarily and wait for completion.
         */
        spin_unlock_irqrestore(&optee->notif.lock, flags);
-       wait_for_completion(&entry->c);
+       if (timeout != 0) {
+               if (!wait_for_completion_timeout(&entry->c, timeout))
+                       rc = -ETIMEDOUT;
+       } else {
+               wait_for_completion(&entry->c);
+       }
        spin_lock_irqsave(&optee->notif.lock, flags);
 
        list_del(&entry->link);
drivers/tee/optee/optee_private.h +1 -1
index 7a5243c78b55..da990c4016ec 100644
@@ -252,7 +252,7 @@ struct optee_call_ctx {
 
 int optee_notif_init(struct optee *optee, u_int max_key);
 void optee_notif_uninit(struct optee *optee);
-int optee_notif_wait(struct optee *optee, u_int key);
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);
 int optee_notif_send(struct optee *optee, u_int key);
 
 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
drivers/tee/optee/rpc.c +8 -3
index e69bc6380683..14e6246aaf05 100644
@@ -130,6 +130,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
 static void handle_rpc_func_cmd_wq(struct optee *optee,
                                   struct optee_msg_arg *arg)
 {
+       int rc = 0;
+
        if (arg->num_params != 1)
                goto bad;
 
@@ -139,7 +141,8 @@ static void handle_rpc_func_cmd_wq(struct optee *optee,
 
        switch (arg->params[0].u.value.a) {
        case OPTEE_RPC_NOTIFICATION_WAIT:
-               if (optee_notif_wait(optee, arg->params[0].u.value.b))
+               rc = optee_notif_wait(optee, arg->params[0].u.value.b, arg->params[0].u.value.c);
+               if (rc)
                        goto bad;
                break;
        case OPTEE_RPC_NOTIFICATION_SEND:
@@ -153,7 +156,10 @@ static void handle_rpc_func_cmd_wq(struct optee *optee,
        arg->ret = TEEC_SUCCESS;
        return;
 bad:
-       arg->ret = TEEC_ERROR_BAD_PARAMETERS;
+       if (rc == -ETIMEDOUT)
+               arg->ret = TEEC_ERROR_BUSY;
+       else
+               arg->ret = TEEC_ERROR_BAD_PARAMETERS;
 }
 
 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
--

2.18.0

#4 lore raw

Hi Jens,

Thanks very much for the reviewing.

Quoted 18 lines · Jens Wiklander

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 6 lines · gavin.liu

From: Gavin Liu <[email protected]>

Add wq_sleep_timeout to support self waking when timeout for secure driver usage.

Quoted 6 lines
---
 drivers/tee/optee/notif.c         |  9 +++++++--
 drivers/tee/optee/optee_private.h |  2 +-
 drivers/tee/optee/rpc.c           | 10 ++++++++--
 3 files changed, 16 insertions(+), 5 deletions(-)
Quoted 6 lines

I'd like to see the corresponding secure world changes taking advantage of this ABI change before we take this any further.

Thanks,
Jens

I just sent a PR for the secure world change.

Quoted 89 lines

drivers/tee/optee/notif.c static bool have_key(struct optee *optee, u_int

Quoted 6 lines
drivers/tee/optee/notif.c +0 -0
index 05212842b0a5..d5e5c0645609 100644
@@ -29,7 +29,7 @@ static bool have_key(struct optee *optee, u_int

key)

drivers/tee/optee/notif.c int optee_notif_wait(struct optee *optee, u_int

Quoted 9 lines
drivers/tee/optee/notif.c +1 -1
        return false;
 }
 
-int optee_notif_wait(struct optee *optee, u_int key)
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout)
 {
        unsigned long flags;
        struct notif_entry *entry;
@@ -70,7 +70,12 @@ int optee_notif_wait(struct optee *optee, u_int

key)

drivers/tee/optee/notif.c int optee_notif_wait(struct optee *optee, u_int key)

Quoted 6 lines
drivers/tee/optee/notif.c +2 -1
         * Unlock temporarily and wait for completion.
         */
        spin_unlock_irqrestore(&optee->notif.lock, flags);
-       wait_for_completion(&entry->c);
+       if (timeout != 0) {
+               if (!wait_for_completion_timeout(&entry->c,

timeout))

Review

Quoted 8 lines
drivers/tee/optee/notif.c +4 -0
+                       rc = -ETIMEDOUT;
+       } else {
+               wait_for_completion(&entry->c);
+       }
        spin_lock_irqsave(&optee->notif.lock, flags);
 
        list_del(&entry->link);
(quoted diff) +0 -0

b/drivers/tee/optee/optee_private.h

drivers/tee/optee/optee_private.h struct optee_call_ctx { · line 258

Quoted 12 lines

index 7a5243c78b55..da990c4016ec 100644

drivers/tee/optee/optee_private.h +1 -1
@@ -252,7 +252,7 @@ struct optee_call_ctx {
 
 int optee_notif_init(struct optee *optee, u_int max_key);
 void optee_notif_uninit(struct optee *optee);
-int optee_notif_wait(struct optee *optee, u_int key);
+int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);
 int optee_notif_send(struct optee *optee, u_int key);
 
 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t

num_params,

drivers/tee/optee/rpc.c static void

Quoted 5 lines
drivers/tee/optee/rpc.c +0 -0
index e69bc6380683..14e6246aaf05 100644
@@ -130,6 +130,8 @@ static void

handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,

drivers/tee/optee/rpc.c static void handle_rpc_func_cmd_wq(struct optee

Quoted 9 lines
drivers/tee/optee/rpc.c +2 -0
 static void handle_rpc_func_cmd_wq(struct optee *optee,
                                   struct optee_msg_arg *arg)
 {
+       int rc = 0;
+
        if (arg->num_params != 1)
                goto bad;
 
@@ -139,7 +141,8 @@ static void handle_rpc_func_cmd_wq(struct optee

*optee,

drivers/tee/optee/rpc.c static void handle_rpc_func_cmd_wq(struct

Quoted 12 lines
drivers/tee/optee/rpc.c +2 -1
        switch (arg->params[0].u.value.a) {
        case OPTEE_RPC_NOTIFICATION_WAIT:
-               if (optee_notif_wait(optee, arg-
 params[0].u.value.b))
+               rc = optee_notif_wait(optee, arg-
 params[0].u.value.b, arg->params[0].u.value.c);
+               if (rc)
                        goto bad;
                break;

case OPTEE_RPC_NOTIFICATION_SEND:

drivers/tee/optee/rpc.c +0 -0
@@ -153,7 +156,10 @@ static void handle_rpc_func_cmd_wq(struct

optee *optee,

Quoted 14 lines
drivers/tee/optee/rpc.c +4 -2
        arg->ret = TEEC_SUCCESS;
        return;
 bad:
-       arg->ret = TEEC_ERROR_BAD_PARAMETERS;
+       if (rc == -ETIMEDOUT)
+               arg->ret = TEEC_ERROR_BUSY;
+       else
+               arg->ret = TEEC_ERROR_BAD_PARAMETERS;
 }
 
 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
--

2.18.0