Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp840787rdg; Fri, 13 Oct 2023 02:49:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXYqwNL4Wrut6g9MyvZ90OUeswGq69jhzYuApmLinOqva1SG5PBvNBO0qZoCA9eqB5Mxja X-Received: by 2002:a05:6a00:a18:b0:690:28d0:b7b3 with SMTP id p24-20020a056a000a1800b0069028d0b7b3mr29056942pfh.13.1697190568735; Fri, 13 Oct 2023 02:49:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697190568; cv=none; d=google.com; s=arc-20160816; b=jw4dycpbnh0ZEau6QRdIgY+FlH3OGxMFqQSpAp/oWqZ7xPcUMFrQSOpI9J6JW9Quzi gh+yyf5ZYhQ2IkIYl4mk8bhSEPKad2X+7DOF1hWJfPmHaw1VH2U5u+edAdTFYr3eDkSR kg7NKhmltoMqY+uiYnrF9VvLbxRtTJ87svYxkRJvOyyFE22FK7s+b29u5euLRdF/2lkJ cDj2FUVMbdDsPICqdkZbRa6ce31fXSQDBx9qpbF3OT9XPPv42iepN/fDAaHPxSI3hNNm uLdsSHSqXB9c04MsDlU8EeXYBTjwMZNLHSNdBDTg5k6M5GOtEPMhjPUhPkzIvYeinv6b QoSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=XIJllFa3lyrGANH10umtce4GqB7bYAU7W5gGSo/oanM=; fh=WvvV6j4z4YwUmvCrqbSKjQ7SZliHDKOBlUMbbAWexc4=; b=BM91yK7Cy02SGy/iogJqfExP3SvmtT3fpfD9NYjryuNmSBNnJ/guWecNW0JxJT1FRB +dIdCBpmhDetlUUbQ2P4HgNOl0Q0eep9NFOw3nw5MxZupVyXSn+4FVx0NNy752HjCuD1 j/+eh3jX4P8WhQr3KYwiJV6GP/IyfYAOKRNg12jhLysY/7f7G2T+eDd0gh/1JmRqXfHM SexXqrWDYJmD7O8rQFA4t3+m0puJyN2ZK6KLlzUO82aylFEndbgAfeeOvKNRvy7FkZYZ 4+CX9sQfJ/PTqU+Mp6LsMzDDPVGxv+Z03VdmUApYv5YQ5AxcBj1K3rI99JfHZqIbioS9 TXgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=kSEbrAdo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id cg5-20020a056a00290500b0068a692b67b0si16203408pfb.104.2023.10.13.02.49.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 02:49:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=kSEbrAdo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 44273822E54E; Fri, 13 Oct 2023 02:49:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231176AbjJMJtP (ORCPT + 99 others); Fri, 13 Oct 2023 05:49:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230120AbjJMJtO (ORCPT ); Fri, 13 Oct 2023 05:49:14 -0400 Received: from mx08-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 479FE95 for ; Fri, 13 Oct 2023 02:49:12 -0700 (PDT) Received: from pps.filterd (m0369457.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 39D9SVN8004572; Fri, 13 Oct 2023 11:49:03 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h= from:to:cc:subject:date:message-id:references:in-reply-to :content-type:content-transfer-encoding:mime-version; s= selector1; bh=XIJllFa3lyrGANH10umtce4GqB7bYAU7W5gGSo/oanM=; b=kS EbrAdoW/ajKQZOaFa4aEjskTCZc6GkQfC2kOo7Xob43CKKQIrimm+u6yM2y7Rd73 irTAv5M8hH47Uv0klQ2wj7dBndp8C0UlVIfb0FFqoSHQ1u13c5NVQVoEvFRCfczY w8Le1kmRkmLVLLEhWma4n9f7HPNIXs8+XDF0G6jRE3ujlvalWYRv6BEXqFBuPDcE /CSpeuzJ7D8xULXZd4zAC55iCPcZi2Ua/U4h7WsuMK7lWOSwWH+BEBeZMQz5XXHR tHv+ReAxX0+EEZAIuU6MGhpsk7FVuhNZh3q+myy6O1Lm2ZfEkvDctNvvjllbs/0A 9elVHDYzbcmiSEboBuLw== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3tkj9h89np-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 Oct 2023 11:49:02 +0200 (MEST) Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 0F269100056; Fri, 13 Oct 2023 11:49:01 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node2.st.com [10.75.129.70]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id E732121A208; Fri, 13 Oct 2023 11:49:01 +0200 (CEST) Received: from SHFDAG1NODE1.st.com (10.75.129.69) by SHFDAG1NODE2.st.com (10.75.129.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Fri, 13 Oct 2023 11:48:59 +0200 Received: from SHFDAG1NODE1.st.com ([fe80::117e:c4ab:ed81:6cb1]) by SHFDAG1NODE1.st.com ([fe80::117e:c4ab:ed81:6cb1%14]) with mapi id 15.01.2507.027; Fri, 13 Oct 2023 11:48:59 +0200 From: Etienne CARRIERE - foss To: Sumit Garg CC: "linux-kernel@vger.kernel.org" , "Jens Wiklander" , "linux-arm-kernel@lists.infradead.org" , "op-tee@lists.trustedfirmware.org" , Jerome Forissier Subject: Re: [PATCH v10 3/4] tee: optee: support tracking system threads Thread-Topic: [PATCH v10 3/4] tee: optee: support tracking system threads Thread-Index: AQHZ9gLa0eDXIY4qt063d7Hb9IRsn7A8YsSAgAfLecSAAxABgIAANORE///qbICAACKwav//45oAgAAkeqw= Date: Fri, 13 Oct 2023 09:48:59 +0000 Message-ID: <62e525fefd934329822a16edd9b6b5ab@foss.st.com> References: <20231003140637.31346-1-etienne.carriere@foss.st.com> <20231003140637.31346-4-etienne.carriere@foss.st.com> <5ba5ab8d2cc7477588dd0f6c7531cf16@foss.st.com> , In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.201.20.20] Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-13_03,2023-10-12_01,2023-05-22_02 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email 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 (pete.vger.email [0.0.0.0]); Fri, 13 Oct 2023 02:49:25 -0700 (PDT) > From: Sumit Garg > Sent: Friday, October 13, 2023 11:36 AM >=20 > 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 en= abled, > > > > > > > > that is when TEE describes how many thread context it suppo= rts > > > > > > > > and when at least 1 session has registered as a system sess= ion > > > > > > > > (using tee_client_system_session()). > > > > > > > > > > > > > > > > For sake of simplicity, initialization of call queue manage= ment > > > > > > > > is factorized into new helper function optee_cq_init(). > > > > > > > > > > > > > > > > The SMC ABI part of the driver enables this tracking, but t= he > > > > > > > > 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 provisionin= g. We reserve > > > > > > > > a TEE thread context for system session only when there i= s 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 syste= m thread from > > > > > > > > being resumed if the executing system thread wakes a norm= al waiter in > > > > > > > > the list. > > > > > > > > > > > > > > How would that be possible? The system thread wakeup > > > > > > > (free_thread_threshold =3D 0) is given priority over normal t= hread > > > > > > > wakeup (free_thread_threshold =3D 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 si= ngle queue list. > > > > > > To have a chance to reach the TEE, a waiting thread must wait t= hat 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 s= ide (::total_thread_count=3D10 at init), > > > > > > and several TEE clients in Linux OS, including 2 system session= s, from 2 consumer drivers (::sys_thread_req_count=3D2). > > > > > > > > > > > > Imagine the 9 normal threads and the 1 system thread are in use= . (::free_thread_count=3D0), > > > > > > Now comes the other system session: it goes to the waitqueue li= st. > > > > > > 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=3D1, and calls c= omplete() for the waitequeue. > > > > > > The 1st element in the waitqueue list is the last entered norma= l session invocation. > > > > > > However, that waiter won't switch local boolean 'need_wait' to= false because ::free_thread_count=3D1 and ::sys_thread_req_count!=3D0. > > > > > > So no attempt to reach TEE and wake another waiter on return. > > > > > > At that point there is a system session in the waitqueue list t= hat could enter TEE (::free_thread_count=3D1) but is waiting someone return= s from the TEE. > > > > > > > > > > I suppose the following loop tries to wake-up every waiter to giv= e > > > > > 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 li= st 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 =3D 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)) >=20 > Ack. >=20 > > > > That said, is it better to have 2 lists or to have 1 list possibly scan= ned twice? >=20 > I would prefer to reuse the existing queue. Ok, I'll do. BR, Etienne >=20 > -Sumit >=20 > > 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) >=20