From e636c8d336489bf3eed5878299e6cc045bbad077 Mon Sep 17 00:00:00 2001
From: hc <hc@nodka.com>
Date: Tue, 20 Feb 2024 01:17:29 +0000
Subject: [PATCH] debug lk

---
 kernel/drivers/tee/optee/supp.c |   43 +++++++++++++++++++++++++++++++------------
 1 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/kernel/drivers/tee/optee/supp.c b/kernel/drivers/tee/optee/supp.c
index 43626e1..2d556b7 100644
--- a/kernel/drivers/tee/optee/supp.c
+++ b/kernel/drivers/tee/optee/supp.c
@@ -1,15 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright (c) 2015, Linaro Limited
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
  */
 #include <linux/device.h>
 #include <linux/slab.h>
@@ -88,10 +79,21 @@
 {
 	struct optee *optee = tee_get_drvdata(ctx->teedev);
 	struct optee_supp *supp = &optee->supp;
-	struct optee_supp_req *req = kzalloc(sizeof(*req), GFP_KERNEL);
+	struct optee_supp_req *req;
 	bool interruptable;
 	u32 ret;
+	unsigned long timeleft;
+	int id;
+	struct optee_supp_req *get_req;
 
+	/*
+	 * Return in case there is no supplicant available and
+	 * non-blocking request.
+	 */
+	if (!supp->ctx && ctx->supp_nowait)
+		return TEEC_ERROR_COMMUNICATION;
+
+	req = kzalloc(sizeof(*req), GFP_KERNEL);
 	if (!req)
 		return TEEC_ERROR_OUT_OF_MEMORY;
 
@@ -115,8 +117,17 @@
 	 * exclusive access again.
 	 */
 	while (wait_for_completion_interruptible(&req->c)) {
+		pr_err("Warning, Interrupting an RPC to supplicant!\n");
+		timeleft = wait_for_completion_timeout(&req->c, msecs_to_jiffies(2000));
+		if (timeleft) {
+			/* get completion, it means tee-supplicant is alive. */
+			break;
+		} else {
+			/* timeout, it means tee-supplicant is dead, interrupting an RPC. */
+			interruptable = true;
+		}
+
 		mutex_lock(&supp->mutex);
-		interruptable = !supp->ctx;
 		if (interruptable) {
 			/*
 			 * There's no supplicant available and since the
@@ -135,6 +146,14 @@
 				list_del(&req->link);
 				req->in_queue = false;
 			}
+
+			idr_for_each_entry(&supp->idr, get_req, id) {
+				if (get_req == req) {
+					idr_remove(&supp->idr, id);
+					supp->req_id = -1;
+					break;
+				}
+			}
 		}
 		mutex_unlock(&supp->mutex);
 

--
Gitblit v1.6.2