Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp835903rdg; Fri, 13 Oct 2023 02:36:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdkyBIPc0urdtStnhEssALcCqnCir0A9Px1ZpGi0ildBRt9DaFxAjOKarPyggSaX+Wnnul X-Received: by 2002:a05:6a00:138e:b0:68f:c2f7:cef0 with SMTP id t14-20020a056a00138e00b0068fc2f7cef0mr40347662pfg.4.1697189791601; Fri, 13 Oct 2023 02:36:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697189791; cv=none; d=google.com; s=arc-20160816; b=bp8IPXc1HaPtUJGKzUAad4jEVUwj8WdNOMm9VmhV04pOfQCaAkWRJT26g/khIKl7Y/ uzkfRrj4kGO3OHR4+HQsS0Ouve/wxcQYBDc7CZQZUe/cArFi6KozhiUCdqjiB5LAoV6K eSa1E0hGn9UV7TTzsbhT7xJjHxl2ZNnksoEdWGXFGXtwz4xyy0ijXENakWubtEVQWv3M XyoOB6whY8pDQmvAXKjDNzqc2QYPU6RKUSXzg/hS35pprB//+W1BV6xDLLaaBFI3jNzN H+J41JOvA8G2l8wYCaQ6s5Oml8yumX4QdP767g19VAStRWm3HABj+/GxhMwQCIvOr4GS mq7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=lWrKfgO6GCFnvp3qj4VKtZbTF6bfPvNcj2aZlqJdVVA=; fh=H9IL18GBn9g1hq+q+7FP75t7A2T+xFNiH1Vf5Tr4Tlk=; b=bZIbyW5MeO11f2S/0kgMHDnWbXI54bu/wjrKHRqAvL/Snqqx3n1PppF1HkbL30bsoY GM5srPAWligEuZb0wUXu+aFHuAjdKPHufrhtYTsSMq5iYYsByp0wcnL9X9QsLHezSucU 65ZQ7IZd340ofmXXkv+WHYLUvGh7Xl12coL2bDbuA+Pw2GuVvNvUt6eXU4hg2MTUdSiS fCBca6qXku5+a+xZk3mAlCUxTF35bfZWtvy/IB4T7DT/bvyCJN+TWcaor64KD6SO8Mmt X9/0vKaUJwKs4xTtz+2gW0zU2L54Jh+xDKP8QRDXRU/HBwnoY/thUT4tXq3vCuvtJtZe yICA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O+5OYzRQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id d185-20020a6336c2000000b00578086d1387si4093567pga.74.2023.10.13.02.36.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 02:36:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O+5OYzRQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 9078382F7F90; Fri, 13 Oct 2023 02:36:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230526AbjJMJg0 (ORCPT + 99 others); Fri, 13 Oct 2023 05:36:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230424AbjJMJgZ (ORCPT ); Fri, 13 Oct 2023 05:36:25 -0400 Received: from mail-vs1-xe36.google.com (mail-vs1-xe36.google.com [IPv6:2607:f8b0:4864:20::e36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40CB4B7 for ; Fri, 13 Oct 2023 02:36:23 -0700 (PDT) Received: by mail-vs1-xe36.google.com with SMTP id ada2fe7eead31-4526b9404b0so2003942137.0 for ; Fri, 13 Oct 2023 02:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697189782; x=1697794582; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=lWrKfgO6GCFnvp3qj4VKtZbTF6bfPvNcj2aZlqJdVVA=; b=O+5OYzRQvL0Cg6V4sZ0m75Lod0Ax1XpeLkxLjt4Tb8BNPIY7+EepckrlgxPZjFj0Y9 kHkaMmQHRX7U0pIU9/Phw67wPN3d2XGjpYcZc+reNDMDX/IzYsf9WclbmPgYFkbSM8uZ TCk6zfJg3YEOZG+DctvuOens265EzBzYRwUyhEnDRElm8yhVnI61xjDJrz3suoUliSV6 2Et8Khsqn8W9Y4bBofGCQupqoMm9H+SIkKGqdQjl4ceYoIgSXbceQGqN+Es6+mTQpwzZ AsEI5fbp5kre2mGVKbrVe/WiaCAL19K/BVFvI2OIkdR9rw9BdDzJwxiXAhzXtNzj4MNF /6Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697189782; x=1697794582; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lWrKfgO6GCFnvp3qj4VKtZbTF6bfPvNcj2aZlqJdVVA=; b=bdcKUVEXOuLLoYT6jlZ4ZblXQEBRGf/aMXA0ilnnMTMfM+yH7/h+JwYe3xEipwyi1V /Un7HtAC6Ns5HWa2OCmhMomNfSvwfXonEfSYJBU2HLhTrWIT0MCWCIh4dUIvf2WLP4Q6 Ox5oJWVPsFQL/6Homq3QE4Yfpr5kUDyKnY32xyBZIluSjwzeemDB+izN+2PWx+3mODfx rOK3+PAhnbpHwhE0opvvVZtoqZVDG/NbqPPjfRhbRHvj6SLMvlClTGnZUNu3AyYQi76R TxyVAztSamkithlbZE067Ajd63sRP9sTdjX4HufzHmpisxQeA5NyJTKOf6ZrCcAEitCH LObQ== X-Gm-Message-State: AOJu0Yytzth2ouBcs1Jsg/hSxdcvoTXle049u5MPTZU4mYGHwfvZ6K3D RCw1xYSE3+KDmIlbaFbJzszsUVtWemTmOvoEXQoWRQ== X-Received: by 2002:a05:6102:3a13:b0:452:5a50:e0ff with SMTP id b19-20020a0561023a1300b004525a50e0ffmr15707753vsu.17.1697189782255; Fri, 13 Oct 2023 02:36:22 -0700 (PDT) MIME-Version: 1.0 References: <20231003140637.31346-1-etienne.carriere@foss.st.com> <20231003140637.31346-4-etienne.carriere@foss.st.com> <5ba5ab8d2cc7477588dd0f6c7531cf16@foss.st.com> In-Reply-To: From: Sumit Garg Date: Fri, 13 Oct 2023 15:06:10 +0530 Message-ID: Subject: Re: [PATCH v10 3/4] tee: optee: support tracking system threads To: Etienne CARRIERE - foss Cc: "linux-kernel@vger.kernel.org" , Jens Wiklander , "linux-arm-kernel@lists.infradead.org" , "op-tee@lists.trustedfirmware.org" , Jerome Forissier Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 13 Oct 2023 02:36:29 -0700 (PDT) On Fri, 13 Oct 2023 at 14:53, Etienne CARRIERE - foss wrote: > > > From: Sumit Garg > > Sent: Friday, October 13, 2023 11:13 AM > > > > On Fri, 13 Oct 2023 at 14:09, Etienne CARRIERE - foss > > wrote: > > > > > > > From: Sumit Garg > > > > Sent: Friday, October 13, 2023 9:21 AM > > > > > > > > On Wed, 11 Oct 2023 at 12:41, Etienne CARRIERE - foss > > > > wrote: > > > > > > > > > > > From: Sumit Garg > > > > > > Sent: Friday, October 6, 2023 11:33 AM > > > > > > > > > > > > On Tue, 3 Oct 2023 at 19:36, Etienne Carriere > > > > > > wrote: > > > > > > > > > > > > > > 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 > > > > > > > Signed-off-by: Jens Wiklander > > > > > > > Co-developed-by: Sumit Garg > > > > > > > Signed-off-by: Sumit Garg > > > > > > > Signed-off-by: Etienne Carriere > > > > > > > --- > > > > > > > Changes since v9: > > > > > > > - Add a reference counter for TEE system thread provisioning. We reserve > > > > > > > a TEE thread context for system session only when there is at least > > > > > > > 1 opened system session. > > > > > > > - Use 2 wait queue lists, normal_waiters and sys_waiter, as proposed in > > > > > > > patch v8. Using a single list can prevent a waiting system thread from > > > > > > > being resumed if the executing system thread wakes a normal waiter in > > > > > > > the list. > > > > > > > > > > > > How would that be possible? The system thread wakeup > > > > > > (free_thread_threshold = 0) is given priority over normal thread > > > > > > wakeup (free_thread_threshold = 1). I think a single queue list would > > > > > > be sufficient as demonstrated in v9. > > > > > > > > > > > > > > > > Hello Sumit, > > > > > > > > > > I think a system session can be trapped waiting when using a single queue list. > > > > > To have a chance to reach the TEE, a waiting thread must wait that a TEE thread comes out of the TEE and calls complete() on the waitqueue to wake next waiter. > > > > > > > > > > To illustrate, consider a 10 TEE threads configuration on TEE side (::total_thread_count=10 at init), > > > > > and several TEE clients in Linux OS, including 2 system sessions, from 2 consumer drivers (::sys_thread_req_count=2). > > > > > > > > > > Imagine the 9 normal threads and the 1 system thread are in use. (::free_thread_count=0), > > > > > Now comes the other system session: it goes to the waitqueue list. > > > > > Now comes a normal session invocation: it goes to the waitqueue list, 1st position. > > > > > > > > > > Now, TEE system thread returns to Linux: > > > > > It increments the counter, ::free_thread_count=1, and calls complete() for the waitequeue. > > > > > The 1st element in the waitqueue list is the last entered normal session invocation. > > > > > However, that waiter won't switch local boolean 'need_wait' to false because ::free_thread_count=1 and ::sys_thread_req_count!=0. > > > > > So no attempt to reach TEE and wake another waiter on return. > > > > > At that point there is a system session in the waitqueue list that could enter TEE (::free_thread_count=1) but is waiting someone returns from the TEE. > > > > > > > > I suppose the following loop tries to wake-up every waiter to give > > > > them a chance to enter OP-TEE. So with that system session would > > > > always be prefered over normal session, right? > > > > > > No, the below loop will wake only the 1st waiter it finds in the list that is > > > current waiting (completion_done() returns false). So if it finds a normal > > > session, it will only wake this one which, in turn, will not try to reach the > > > TEE from the while(need_wiat) loop in optee_cq_wait_init(), because there is > > > not enough free threads. Because it does not reach the TEE, it will not > > > it wake another waiter. > > > > > > > Okay I see your point, so how about the following change on top of v9. > > I still think having 2 queues is an overkill here. > > > > diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c > > index df5fb5410b72..47f57054d9b7 100644 > > --- a/drivers/tee/optee/call.c > > +++ b/drivers/tee/optee/call.c > > @@ -60,6 +60,7 @@ void optee_cq_wait_init(struct optee_call_queue *cq, > > */ > > init_completion(&w->c); > > list_add_tail(&w->list_node, &cq->waiters); > > + w->sys_thread = sys_thread; > > > > ... > > > > @@ -83,6 +84,14 @@ static void optee_cq_complete_one(struct > > optee_call_queue *cq) > > { > > struct optee_call_waiter *w; > > > > + /* Try to wakeup system session capable threads first */ > > + list_for_each_entry(w, &cq->waiters, list_node) { > > + if (!completion_done(&w->c) && w->sys_thread) { > > + complete(&w->c); > > + return; > > + } > > + } > > + > > Indeed, looking for system sessions first in the list would address the issue. > I would test sys_thread firs: if (w->sys_thread && !completion_done(&w->c)) Ack. > > That said, is it better to have 2 lists or to have 1 list possibly scanned twice? I would prefer to reuse the existing queue. -Sumit > I'm fine with both ways. > > etienne > > > > list_for_each_entry(w, &cq->waiters, list_node) { > > if (!completion_done(&w->c)) { > > complete(&w->c); > > diff --git a/drivers/tee/optee/optee_private.h > > b/drivers/tee/optee/optee_private.h > > index 6bb5cae09688..a7817ce9f90f 100644 > > --- a/drivers/tee/optee/optee_private.h > > +++ b/drivers/tee/optee/optee_private.h > > @@ -43,6 +43,7 @@ typedef void (optee_invoke_fn)(unsigned long, > > unsigned long, unsigned long, > > struct optee_call_waiter { > > struct list_head list_node; > > struct completion c; > > + bool sys_thread; > > }; > > > > struct optee_call_queue { > > > > -Sumit > > > > > > > > > > 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) { > > > > if (!completion_done(&w->c)) { > > > > complete(&w->c); > > > > break; > > > > } > > > > } > > > > } > > > > > > > > -Sumit > > > > > > > > > > Note I've found a error in this patch v10, see below. > > > > > > BR, > > > Etienne > > > > > > > > > > > > > > > > With 2 lists, we first treat system sessions to overcome that. > > > > > Am I missing something? > > > > > > > > > > Best regards, > > > > > Etienne > > > > > > > > > > > -Sumit > > > > > > > (snip)