From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7E59C77B75 for ; Tue, 16 May 2023 07:49:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231465AbjEPHtg (ORCPT ); Tue, 16 May 2023 03:49:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjEPHte (ORCPT ); Tue, 16 May 2023 03:49:34 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 419201FCC for ; Tue, 16 May 2023 00:49:32 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-965e93f915aso2466108066b.2 for ; Tue, 16 May 2023 00:49:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684223371; x=1686815371; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=H9MeMI4M1yE9VAFv/Uu8XvJq52cKiBIHNdS2LuQU034=; b=fmlndFJZ6zTztaSTW4ikX1ZZ9GU3mr5IGv/2yPL3g/n6hrtyll5hNRVWPSzh/bu0vk yMsGAHo6ufA9OJ1Dh8HaTAmbnneMgfwcLP4aRVYJwiB6sX5ih6SY5hC4edTxaO6fk+fR GIadiZ3ZTK5788BfR8lItXzSWWsCPIwSiHTDfipPCJWtnfon/08tuqG+WKDvo2VUTrts hAF0NnTTt5lwUoGilwb7dF7zUcWTz1d0LZ5/Ph7XYpMTxA34v73WxstrA9GoW3po9L1z efW89cF9lHjSTPk4otG9fbgVeuD6eVWlZscV+HLFYUFybMw762TULIzoxmDOC2MoWPkE ivcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684223371; x=1686815371; 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=H9MeMI4M1yE9VAFv/Uu8XvJq52cKiBIHNdS2LuQU034=; b=Puqimybh1cUTgIBwrt06Mcsa/8Yf3z9nQ2nHVV86wjvkN1nW4mDP5kUQLR4mkyIAli e65v32pT56MwdISdRtazIpO26EtfJWp4keIBpowrS1Ojt3kTID+jzFXFXuQy9s0WIu98 4EhqivI6vyHsQqZ9H8/CBVWuqN0vjJxpmPL/OkDEWJ0ZuDtitCH5kXCeTzApWbQ/MKbG xEWDRw/SxSzUWecGtEx36rrTRstzCLzOJ4lPVBYFjN95H30XpR/9O6rieVhpt6kTCY8Q ku6Xe5qtXQ6lKDviZrPJ6E6f7yZ6VrKpvVHtSjFE1Esqm9lDDtLqt+QznfsbnEaxrA4M EhNg== X-Gm-Message-State: AC+VfDzZvRCq3yGbViN1PSnbIHHQ1XXv5qROATeErUwEy9GthNV2z33K qVdAwHizCnhchKz3iDFOavVi9DqCTUlsjrDEG/ng/A== X-Google-Smtp-Source: ACHHUZ6flr188Ins4PCZV6HJTpyrl9abssnnk9/twTOF86oVhK4ennGAhXLLE96bivWkD5Wae7O9kekCFWoXuPz6dQg= X-Received: by 2002:a17:906:7949:b0:96a:d916:cb31 with SMTP id l9-20020a170906794900b0096ad916cb31mr9762444ejo.29.1684223370604; Tue, 16 May 2023 00:49:30 -0700 (PDT) MIME-Version: 1.0 References: <20230505173012.881083-1-etienne.carriere@linaro.org> <20230505173012.881083-3-etienne.carriere@linaro.org> In-Reply-To: From: Etienne Carriere Date: Tue, 16 May 2023 09:49:19 +0200 Message-ID: Subject: Re: [PATCH v6 3/4] tee: optee: support tracking system threads To: Sumit Garg Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org, Jens Wiklander , Sudeep Holla , Cristian Marussi Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > > > > > > > > > > > > These changes do not define an overall single system thread. > > > > > > If several sessions requests reservation of TEE system thread, has > > > > > > many will be reserved. > > > > > > Only the very sessions with its sys_thread attribute set will use a > > > > > > reserved thread. If such a kernel client issues several concurrent > > > > > > calls to OP-TEE over that session, it will indeed consume more > > > > > > reserved system threads than what is actually reserved. Here I think > > > > > > it is the responsibility of such client to open as many sessions as > > > > > > requests. This is what scmi/optee driver does (see patch v6 4/4). An > > > > > > alternative would be to have a ref count of sys_thread in session > > > > > > contexts rather than a boolean value. I don't think it's worth it. > > > > > > > > > > Ah, I missed that during the review. The invocations with system > > > > > threads should be limited by res_sys_thread_count in a similar manner > > > > > as we do with normal threads via free_normal_thread_count. Otherwise, > > > > > it's unfair for normal thread scheduling. > > > > > > > > > > I suppose there isn't any interdependency among SCMI channels itself > > > > > such that a particular SCMI invocation can wait until the other SCMI > > > > > invocation has completed. > > > > > > > > I think that would over complexify the logic. > > > > > > > > > > We shouldn't allow system thread invocations to be greater than what > > > is actually reserved count for system threads. One thing I am not able > > > to understand here is why do you need a lot of system threads? Are > > > SCMI operations too expensive? I suppose those should just involve > > > configuring some register bits and using a single OP-TEE thread which > > > is invoked sequentially should be enough. > > > > Ok, I get your point. > > I think you're right, reserving at most 1 TEE thread for system > > sessions should be enough to prevent TEE entry calls deadlocks which > > is the purpose of these changee. > > > > Would you be ok if the following logic: optee driver would reserve at > > most 1 TEE call entry for system sessions. > > If at least 1 kernel client claims a system session, a TEE call entry > > is reserved to that purpose. > > Once all system sessions are closed, the TEE reserved system call > > entry is released. > > When a system thread calls the TEE, if the TEE system thread context > > is not already in use, then that client consumes the reserved entry. > > If the system thread context is already in use, then that client call > > is treated as a regular call: it calls the TEE and would return > > waiting for a free thread if no TEE thread context is available. > > Yeah this sounds reasonable to me. > Ok, i'll address that in patch v8. Thanks. Etienne > -Sumit > > > > > Etienne > > > > > > > > > > -Sumit > > > > > > > Note I will send a patch v8 series but feel free to continue the discussion. > > > > It will at least address other comments you shared. > > > > > > > > Best regards, > > > > Etienne > > > > > > > > > > > > > > -Sumit On Tue, 16 May 2023 at 08:32, Sumit Garg wrote: > > On Tue, 16 May 2023 at 11:28, Etienne Carriere > wrote: > > > > Hello Sumit, > > > > On Mon, 15 May 2023 at 10:48, Sumit Garg wrote: > > > > > > On Fri, 12 May 2023 at 10:27, Etienne Carriere > > > wrote: > > > > > > > > On Thu, 11 May 2023 at 13:31, Sumit Garg wrote: > > > > > > > > > > On Thu, 11 May 2023 at 13:49, Etienne Carriere > > > > > wrote: > > > > > > > > > > > > On Thu, 11 May 2023 at 09:27, Sumit Garg wrote: > > > > > > > (snip) > > > > > > > > > > > > > > > > > > > > > > +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 */ > > > > > > > > > > > > > > > > > > > > IMO, this might be counter productive. As most kernel drivers open a > > > > > > > > > > session during driver probe which are only released in the driver > > > > > > > > > > release method. > > > > > > > > > > > > > > > > > > It is always the case? > > > > > > > > > > > > > > > > This answer of mine is irrelevant. Sorry, > > > > > > > > Please read only the below comments of mine, especially: > > > > > > > > | Note that an OP-TEE thread is not bound to a TEE session but rather > > > > > > > > | bound to a yielded call to OP-TEE. > > > > > > > > > > > > > > > > > > > > > > > > > > > If the kernel driver is built-in then the session is > > > > > > > > > > never released. Now with system threads we would reserve an OP-TEE > > > > > > > > > > thread for that kernel driver as well which will never be available to > > > > > > > > > > regular user-space clients. > > > > > > > > > > > > > > > > > > That is not true. No driver currently requests their TEE thread to be > > > > > > > > > a system thread. > > > > > > > > > Only SCMI does because it needs to by construction. > > > > > > > > > > > > > > > > > > > > > > > Yes that's true but what prevents future/current kernel TEE drivers > > > > > > > from requesting a system thread once we have this patch-set landed. > > > > > > > > > > > > Only clients really needing this system_thread attribute should request it. > > > > > > If they really need, the OP-TEE firmware in secure world should > > > > > > provision sufficient thread context. > > > > > > > > > > How do we quantify it? We definitely need a policy here regarding > > > > > normal vs system threads. > > > > > > > > > > One argument in favor of kernel clients requiring system threads could > > > > > be that we don't want to compete with user-space for OP-TEE threads. > > > > > > > > Sorry I don't understand. What do you mean qualifying this? > > > > > > I mean we have to fairly allocate threads among system and non-system > > > thread invocations. > > > > > > > In an ideal situation, we would have OP-TEE provisioned with largely > > > > sufficient thread contexts. However there are systems with constraints > > > > memory resource that do lower at most the number of OP-TEE thread > > > > contexts. > > > > > > > > > > Yeah, I think we are on the same page here. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > So I would rather suggest we only allow a > > > > > > > > > > single system thread to be reserved as a starting point which is > > > > > > > > > > relevant to this critical SCMI service. We can also make this upper > > > > > > > > > > bound for system threads configurable with default value as 1 if > > > > > > > > > > needed. > > > > > > > > > > > > > > > > Note that SCMI server can expose several SCMI channels (at most 1 per > > > > > > > > SCMI protocol used) and each of them will need to request a > > > > > > > > system_thread to TEE driver. > > > > > > > > > > > > > > > > Etienne > > > > > > > > > > > > > > > > > > > > > > > > > > Reserving one or more system threads depends on the number of thread > > > > > > > > > context provisioned by the TEE. > > > > > > > > > Note that the implementation proposed here prevents Linux kernel from > > > > > > > > > exhausting TEE threads so user space always has at least a TEE thread > > > > > > > > > context left available. > > > > > > > > > > > > > > Yeah but on the other hand user-space clients which are comparatively > > > > > > > larger in number than kernel clients. So they will be starved for > > > > > > > OP-TEE thread availability. Consider a user-space client which needs > > > > > > > to serve a lot of TLS connections just waiting for OP-TEE thread > > > > > > > availability. > > > > > > > > > > > > Note that OP-TEE default configuration provisions (number of CPUs + 1) > > > > > > thread context, so the situation is already present before these > > > > > > changes on systems that embedded an OP-TEE without a properly tuned > > > > > > configuration. As I said above, Linux kernel cannot be responsible for > > > > > > the total number of thread contexts provisioned in OP-TEE. If the > > > > > > overall system requires a lot of TEE thread contexts, one should embed > > > > > > a suitable OP-TEE firmware. > > > > > > > > > > Wouldn't the SCMI deadlock problem be solved with just having a lot of > > > > > OP-TEE threads? But we are discussing the system threads solution here > > > > > to make efficient use of OP-TEE threads. The total number of OP-TEE > > > > > threads is definitely in control of OP-TEE but the control of how to > > > > > schedule and efficiently use them lies with the Linux OP-TEE driver. > > > > > > > > > > So, given our overall discussion in this thread, how about the upper > > > > > bound for system threads being 50% of the total number of OP-TEE > > > > > threads? > > > > > > > > What would be a shame if the system does not use any Linux kernel > > > > client sessions, only userland clients. This information cannot be > > > > knwon be the linux optee driver. > > > > Instead of leaving at least 1 TEE thread context for regular session, > > > > what if this change enforce 2? or 3? Which count? > > > > I think 1 is a fair choice: it allows to support OP-TEE firmwares with > > > > a very small thread context pool (when running in small secure > > > > memory), embedding only 2 or 3 contextes. > > > > > > IMO, leaving only 1 thread for user-space will starve TLS based > > > applications. How about the following change on top of this patchset? > > > > > > diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c > > > index 8b8181099da7..1deb5907d075 100644 > > > --- a/drivers/tee/optee/call.c > > > +++ b/drivers/tee/optee/call.c > > > @@ -182,8 +182,8 @@ bool optee_cq_inc_sys_thread_count(struct > > > optee_call_queue *cq) > > > > > > mutex_lock(&cq->mutex); > > > > > > - /* Leave at least 1 normal (non-system) thread */ > > > - if (cq->res_sys_thread_count + 1 < cq->total_thread_count) { > > > + /* Leave at least 50% for normal (non-system) threads */ > > > + if (cq->res_sys_thread_count < cq->total_thread_count/2) { > > > cq->free_normal_thread_count--; > > > cq->res_sys_thread_count++; > > > rc = true; > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Note that an OP-TEE thread is not bound to a TEE session but rather > > > > > > > > > bound to a yielded call to OP-TEE. > > > > > > > > > > > > > > tee_client_open_session() > > > > > > > -> optee_open_session() > > > > > > > > > > > > > > tee_client_system_session() > > > > > > > -> optee_system_session() > > > > > > > -> optee_cq_inc_sys_thread_count() <- At this point you > > > > > > > reserve a system thread corresponding to a particular kernel client > > > > > > > session > > > > > > > > > > > > > > All tee_client_invoke_func() invocations with a system thread capable > > > > > > > session will use that reserved thread. > > > > > > > > > > > > > > tee_client_close_session() > > > > > > > -> optee_close_session() > > > > > > > -> optee_close_session_helper() > > > > > > > -> optee_cq_dec_sys_thread_count() <- At this point the > > > > > > > reserved system thread is released > > > > > > > > > > > > > > Haven't this tied the system thread to a particular TEE session? Or am > > > > > > > I missing something? > > > > > > > > > > > > These changes do not define an overall single system thread. > > > > > > If several sessions requests reservation of TEE system thread, has > > > > > > many will be reserved. > > > > > > Only the very sessions with its sys_thread attribute set will use a > > > > > > reserved thread. If such a kernel client issues several concurrent > > > > > > calls to OP-TEE over that session, it will indeed consume more > > > > > > reserved system threads than what is actually reserved. Here I think > > > > > > it is the responsibility of such client to open as many sessions as > > > > > > requests. This is what scmi/optee driver does (see patch v6 4/4). An > > > > > > alternative would be to have a ref count of sys_thread in session > > > > > > contexts rather than a boolean value. I don't think it's worth it. > > > > > > > > > > Ah, I missed that during the review. The invocations with system > > > > > threads should be limited by res_sys_thread_count in a similar manner > > > > > as we do with normal threads via free_normal_thread_count. Otherwise, > > > > > it's unfair for normal thread scheduling. > > > > > > > > > > I suppose there isn't any interdependency among SCMI channels itself > > > > > such that a particular SCMI invocation can wait until the other SCMI > > > > > invocation has completed. > > > > > > > > I think that would over complexify the logic. > > > > > > > > > > We shouldn't allow system thread invocations to be greater than what > > > is actually reserved count for system threads. One thing I am not able > > > to understand here is why do you need a lot of system threads? Are > > > SCMI operations too expensive? I suppose those should just involve > > > configuring some register bits and using a single OP-TEE thread which > > > is invoked sequentially should be enough. > > > > Ok, I get your point. > > I think you're right, reserving at most 1 TEE thread for system > > sessions should be enough to prevent TEE entry calls deadlocks which > > is the purpose of these changee. > > > > Would you be ok if the following logic: optee driver would reserve at > > most 1 TEE call entry for system sessions. > > If at least 1 kernel client claims a system session, a TEE call entry > > is reserved to that purpose. > > Once all system sessions are closed, the TEE reserved system call > > entry is released. > > When a system thread calls the TEE, if the TEE system thread context > > is not already in use, then that client consumes the reserved entry. > > If the system thread context is already in use, then that client call > > is treated as a regular call: it calls the TEE and would return > > waiting for a free thread if no TEE thread context is available. > > Yeah this sounds reasonable to me. > > -Sumit > > > > > Etienne > > > > > > > > > > -Sumit > > > > > > > Note I will send a patch v8 series but feel free to continue the discussion. > > > > It will at least address other comments you shared. > > > > > > > > Best regards, > > > > Etienne > > > > > > > > > > > > > > -Sumit From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B76F4C77B7A for ; Tue, 16 May 2023 07:50:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=eEP+jsAuUtt1j3icIg3BynIXpVHO6HM7U48VhTX5M+4=; b=UYYfbf7MJLRB8c tPmBOfiiuZUgjEOPwN3nQl47/YNUFspOdVZOY0ZAzLbQZjMWt4nDgHhhJkDQfz2NYP24QyARchw4P HJyHwHQysgkEpkYg9HSsr+eAlaD+OwthF4j50BvVO+qXYTNmOC0dYqF6zUSS9U30J4ivu5gdMZC5P 5/S8MqFd/lL9IDB6zirgr4Bm7s9K5whsS6j1yxT3LJl9YS4g8Im1glbiDXmzo1dA2f8t/xtVAYAYn 0MA9Gyw6PIDYmR6xJbS4J6/kmMuchP0FT6LX0QbMKJyyjddq00wfmF+zj1/XrZ9JjoyW1nuFtAk8u JOlqVbXfMV7ajvB6CRBg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pypRC-004iOn-36; Tue, 16 May 2023 07:49:38 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pypR8-004iNv-1V for linux-arm-kernel@lists.infradead.org; Tue, 16 May 2023 07:49:36 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-966400ee79aso2126701866b.0 for ; Tue, 16 May 2023 00:49:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684223371; x=1686815371; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=H9MeMI4M1yE9VAFv/Uu8XvJq52cKiBIHNdS2LuQU034=; b=fmlndFJZ6zTztaSTW4ikX1ZZ9GU3mr5IGv/2yPL3g/n6hrtyll5hNRVWPSzh/bu0vk yMsGAHo6ufA9OJ1Dh8HaTAmbnneMgfwcLP4aRVYJwiB6sX5ih6SY5hC4edTxaO6fk+fR GIadiZ3ZTK5788BfR8lItXzSWWsCPIwSiHTDfipPCJWtnfon/08tuqG+WKDvo2VUTrts hAF0NnTTt5lwUoGilwb7dF7zUcWTz1d0LZ5/Ph7XYpMTxA34v73WxstrA9GoW3po9L1z efW89cF9lHjSTPk4otG9fbgVeuD6eVWlZscV+HLFYUFybMw762TULIzoxmDOC2MoWPkE ivcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684223371; x=1686815371; 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=H9MeMI4M1yE9VAFv/Uu8XvJq52cKiBIHNdS2LuQU034=; b=LxG/clVpsfZHLBZE/R43UdXHMsMYT4fpoLv5EcWeKBI6DJiUXBCZk97zAYJJ15sPx6 lToldGxI1H2gI5/5H38M5MS+lWREN3VoZFUyUhFhgihefg/Rzkn10WbQCbn77dyLh4Kx 0SrQFvqhWAkmEeuTfxNSviFM+KalqoZc9tbIBEp2EnjPhJv/avyKombE7TpAzdNHfs36 N8x0VqW3QaLj4oKctyVrtbnz1hOBxAF1WJxziWnLE0NEV3vmssfo8Cph/+iYFx2XkdYn +7MxlZl6jT8yZi2iNIxabt3+p/vr9ulHMmQ8hO+x3gGeF2RVxiSfGiamwBW2F2ZEIMLk Mcig== X-Gm-Message-State: AC+VfDxmPGGPMlCz//crveP3AgbogbwtLJP4KQLetJ61+TKVORFeZr6P 9E455CZNSRgRVXI3FpfSwrjG639UUCxjHOkQicRfvw== X-Google-Smtp-Source: ACHHUZ6flr188Ins4PCZV6HJTpyrl9abssnnk9/twTOF86oVhK4ennGAhXLLE96bivWkD5Wae7O9kekCFWoXuPz6dQg= X-Received: by 2002:a17:906:7949:b0:96a:d916:cb31 with SMTP id l9-20020a170906794900b0096ad916cb31mr9762444ejo.29.1684223370604; Tue, 16 May 2023 00:49:30 -0700 (PDT) MIME-Version: 1.0 References: <20230505173012.881083-1-etienne.carriere@linaro.org> <20230505173012.881083-3-etienne.carriere@linaro.org> In-Reply-To: From: Etienne Carriere Date: Tue, 16 May 2023 09:49:19 +0200 Message-ID: Subject: Re: [PATCH v6 3/4] tee: optee: support tracking system threads To: Sumit Garg Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org, Jens Wiklander , Sudeep Holla , Cristian Marussi X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230516_004934_531825_7BA35438 X-CRM114-Status: GOOD ( 73.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org > > > > > > > > > > > > These changes do not define an overall single system thread. > > > > > > If several sessions requests reservation of TEE system thread, has > > > > > > many will be reserved. > > > > > > Only the very sessions with its sys_thread attribute set will use a > > > > > > reserved thread. If such a kernel client issues several concurrent > > > > > > calls to OP-TEE over that session, it will indeed consume more > > > > > > reserved system threads than what is actually reserved. Here I think > > > > > > it is the responsibility of such client to open as many sessions as > > > > > > requests. This is what scmi/optee driver does (see patch v6 4/4). An > > > > > > alternative would be to have a ref count of sys_thread in session > > > > > > contexts rather than a boolean value. I don't think it's worth it. > > > > > > > > > > Ah, I missed that during the review. The invocations with system > > > > > threads should be limited by res_sys_thread_count in a similar manner > > > > > as we do with normal threads via free_normal_thread_count. Otherwise, > > > > > it's unfair for normal thread scheduling. > > > > > > > > > > I suppose there isn't any interdependency among SCMI channels itself > > > > > such that a particular SCMI invocation can wait until the other SCMI > > > > > invocation has completed. > > > > > > > > I think that would over complexify the logic. > > > > > > > > > > We shouldn't allow system thread invocations to be greater than what > > > is actually reserved count for system threads. One thing I am not able > > > to understand here is why do you need a lot of system threads? Are > > > SCMI operations too expensive? I suppose those should just involve > > > configuring some register bits and using a single OP-TEE thread which > > > is invoked sequentially should be enough. > > > > Ok, I get your point. > > I think you're right, reserving at most 1 TEE thread for system > > sessions should be enough to prevent TEE entry calls deadlocks which > > is the purpose of these changee. > > > > Would you be ok if the following logic: optee driver would reserve at > > most 1 TEE call entry for system sessions. > > If at least 1 kernel client claims a system session, a TEE call entry > > is reserved to that purpose. > > Once all system sessions are closed, the TEE reserved system call > > entry is released. > > When a system thread calls the TEE, if the TEE system thread context > > is not already in use, then that client consumes the reserved entry. > > If the system thread context is already in use, then that client call > > is treated as a regular call: it calls the TEE and would return > > waiting for a free thread if no TEE thread context is available. > > Yeah this sounds reasonable to me. > Ok, i'll address that in patch v8. Thanks. Etienne > -Sumit > > > > > Etienne > > > > > > > > > > -Sumit > > > > > > > Note I will send a patch v8 series but feel free to continue the discussion. > > > > It will at least address other comments you shared. > > > > > > > > Best regards, > > > > Etienne > > > > > > > > > > > > > > -Sumit On Tue, 16 May 2023 at 08:32, Sumit Garg wrote: > > On Tue, 16 May 2023 at 11:28, Etienne Carriere > wrote: > > > > Hello Sumit, > > > > On Mon, 15 May 2023 at 10:48, Sumit Garg wrote: > > > > > > On Fri, 12 May 2023 at 10:27, Etienne Carriere > > > wrote: > > > > > > > > On Thu, 11 May 2023 at 13:31, Sumit Garg wrote: > > > > > > > > > > On Thu, 11 May 2023 at 13:49, Etienne Carriere > > > > > wrote: > > > > > > > > > > > > On Thu, 11 May 2023 at 09:27, Sumit Garg wrote: > > > > > > > (snip) > > > > > > > > > > > > > > > > > > > > > > +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 */ > > > > > > > > > > > > > > > > > > > > IMO, this might be counter productive. As most kernel drivers open a > > > > > > > > > > session during driver probe which are only released in the driver > > > > > > > > > > release method. > > > > > > > > > > > > > > > > > > It is always the case? > > > > > > > > > > > > > > > > This answer of mine is irrelevant. Sorry, > > > > > > > > Please read only the below comments of mine, especially: > > > > > > > > | Note that an OP-TEE thread is not bound to a TEE session but rather > > > > > > > > | bound to a yielded call to OP-TEE. > > > > > > > > > > > > > > > > > > > > > > > > > > > If the kernel driver is built-in then the session is > > > > > > > > > > never released. Now with system threads we would reserve an OP-TEE > > > > > > > > > > thread for that kernel driver as well which will never be available to > > > > > > > > > > regular user-space clients. > > > > > > > > > > > > > > > > > > That is not true. No driver currently requests their TEE thread to be > > > > > > > > > a system thread. > > > > > > > > > Only SCMI does because it needs to by construction. > > > > > > > > > > > > > > > > > > > > > > > Yes that's true but what prevents future/current kernel TEE drivers > > > > > > > from requesting a system thread once we have this patch-set landed. > > > > > > > > > > > > Only clients really needing this system_thread attribute should request it. > > > > > > If they really need, the OP-TEE firmware in secure world should > > > > > > provision sufficient thread context. > > > > > > > > > > How do we quantify it? We definitely need a policy here regarding > > > > > normal vs system threads. > > > > > > > > > > One argument in favor of kernel clients requiring system threads could > > > > > be that we don't want to compete with user-space for OP-TEE threads. > > > > > > > > Sorry I don't understand. What do you mean qualifying this? > > > > > > I mean we have to fairly allocate threads among system and non-system > > > thread invocations. > > > > > > > In an ideal situation, we would have OP-TEE provisioned with largely > > > > sufficient thread contexts. However there are systems with constraints > > > > memory resource that do lower at most the number of OP-TEE thread > > > > contexts. > > > > > > > > > > Yeah, I think we are on the same page here. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > So I would rather suggest we only allow a > > > > > > > > > > single system thread to be reserved as a starting point which is > > > > > > > > > > relevant to this critical SCMI service. We can also make this upper > > > > > > > > > > bound for system threads configurable with default value as 1 if > > > > > > > > > > needed. > > > > > > > > > > > > > > > > Note that SCMI server can expose several SCMI channels (at most 1 per > > > > > > > > SCMI protocol used) and each of them will need to request a > > > > > > > > system_thread to TEE driver. > > > > > > > > > > > > > > > > Etienne > > > > > > > > > > > > > > > > > > > > > > > > > > Reserving one or more system threads depends on the number of thread > > > > > > > > > context provisioned by the TEE. > > > > > > > > > Note that the implementation proposed here prevents Linux kernel from > > > > > > > > > exhausting TEE threads so user space always has at least a TEE thread > > > > > > > > > context left available. > > > > > > > > > > > > > > Yeah but on the other hand user-space clients which are comparatively > > > > > > > larger in number than kernel clients. So they will be starved for > > > > > > > OP-TEE thread availability. Consider a user-space client which needs > > > > > > > to serve a lot of TLS connections just waiting for OP-TEE thread > > > > > > > availability. > > > > > > > > > > > > Note that OP-TEE default configuration provisions (number of CPUs + 1) > > > > > > thread context, so the situation is already present before these > > > > > > changes on systems that embedded an OP-TEE without a properly tuned > > > > > > configuration. As I said above, Linux kernel cannot be responsible for > > > > > > the total number of thread contexts provisioned in OP-TEE. If the > > > > > > overall system requires a lot of TEE thread contexts, one should embed > > > > > > a suitable OP-TEE firmware. > > > > > > > > > > Wouldn't the SCMI deadlock problem be solved with just having a lot of > > > > > OP-TEE threads? But we are discussing the system threads solution here > > > > > to make efficient use of OP-TEE threads. The total number of OP-TEE > > > > > threads is definitely in control of OP-TEE but the control of how to > > > > > schedule and efficiently use them lies with the Linux OP-TEE driver. > > > > > > > > > > So, given our overall discussion in this thread, how about the upper > > > > > bound for system threads being 50% of the total number of OP-TEE > > > > > threads? > > > > > > > > What would be a shame if the system does not use any Linux kernel > > > > client sessions, only userland clients. This information cannot be > > > > knwon be the linux optee driver. > > > > Instead of leaving at least 1 TEE thread context for regular session, > > > > what if this change enforce 2? or 3? Which count? > > > > I think 1 is a fair choice: it allows to support OP-TEE firmwares with > > > > a very small thread context pool (when running in small secure > > > > memory), embedding only 2 or 3 contextes. > > > > > > IMO, leaving only 1 thread for user-space will starve TLS based > > > applications. How about the following change on top of this patchset? > > > > > > diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c > > > index 8b8181099da7..1deb5907d075 100644 > > > --- a/drivers/tee/optee/call.c > > > +++ b/drivers/tee/optee/call.c > > > @@ -182,8 +182,8 @@ bool optee_cq_inc_sys_thread_count(struct > > > optee_call_queue *cq) > > > > > > mutex_lock(&cq->mutex); > > > > > > - /* Leave at least 1 normal (non-system) thread */ > > > - if (cq->res_sys_thread_count + 1 < cq->total_thread_count) { > > > + /* Leave at least 50% for normal (non-system) threads */ > > > + if (cq->res_sys_thread_count < cq->total_thread_count/2) { > > > cq->free_normal_thread_count--; > > > cq->res_sys_thread_count++; > > > rc = true; > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Note that an OP-TEE thread is not bound to a TEE session but rather > > > > > > > > > bound to a yielded call to OP-TEE. > > > > > > > > > > > > > > tee_client_open_session() > > > > > > > -> optee_open_session() > > > > > > > > > > > > > > tee_client_system_session() > > > > > > > -> optee_system_session() > > > > > > > -> optee_cq_inc_sys_thread_count() <- At this point you > > > > > > > reserve a system thread corresponding to a particular kernel client > > > > > > > session > > > > > > > > > > > > > > All tee_client_invoke_func() invocations with a system thread capable > > > > > > > session will use that reserved thread. > > > > > > > > > > > > > > tee_client_close_session() > > > > > > > -> optee_close_session() > > > > > > > -> optee_close_session_helper() > > > > > > > -> optee_cq_dec_sys_thread_count() <- At this point the > > > > > > > reserved system thread is released > > > > > > > > > > > > > > Haven't this tied the system thread to a particular TEE session? Or am > > > > > > > I missing something? > > > > > > > > > > > > These changes do not define an overall single system thread. > > > > > > If several sessions requests reservation of TEE system thread, has > > > > > > many will be reserved. > > > > > > Only the very sessions with its sys_thread attribute set will use a > > > > > > reserved thread. If such a kernel client issues several concurrent > > > > > > calls to OP-TEE over that session, it will indeed consume more > > > > > > reserved system threads than what is actually reserved. Here I think > > > > > > it is the responsibility of such client to open as many sessions as > > > > > > requests. This is what scmi/optee driver does (see patch v6 4/4). An > > > > > > alternative would be to have a ref count of sys_thread in session > > > > > > contexts rather than a boolean value. I don't think it's worth it. > > > > > > > > > > Ah, I missed that during the review. The invocations with system > > > > > threads should be limited by res_sys_thread_count in a similar manner > > > > > as we do with normal threads via free_normal_thread_count. Otherwise, > > > > > it's unfair for normal thread scheduling. > > > > > > > > > > I suppose there isn't any interdependency among SCMI channels itself > > > > > such that a particular SCMI invocation can wait until the other SCMI > > > > > invocation has completed. > > > > > > > > I think that would over complexify the logic. > > > > > > > > > > We shouldn't allow system thread invocations to be greater than what > > > is actually reserved count for system threads. One thing I am not able > > > to understand here is why do you need a lot of system threads? Are > > > SCMI operations too expensive? I suppose those should just involve > > > configuring some register bits and using a single OP-TEE thread which > > > is invoked sequentially should be enough. > > > > Ok, I get your point. > > I think you're right, reserving at most 1 TEE thread for system > > sessions should be enough to prevent TEE entry calls deadlocks which > > is the purpose of these changee. > > > > Would you be ok if the following logic: optee driver would reserve at > > most 1 TEE call entry for system sessions. > > If at least 1 kernel client claims a system session, a TEE call entry > > is reserved to that purpose. > > Once all system sessions are closed, the TEE reserved system call > > entry is released. > > When a system thread calls the TEE, if the TEE system thread context > > is not already in use, then that client consumes the reserved entry. > > If the system thread context is already in use, then that client call > > is treated as a regular call: it calls the TEE and would return > > waiting for a free thread if no TEE thread context is available. > > Yeah this sounds reasonable to me. > > -Sumit > > > > > Etienne > > > > > > > > > > -Sumit > > > > > > > Note I will send a patch v8 series but feel free to continue the discussion. > > > > It will at least address other comments you shared. > > > > > > > > Best regards, > > > > Etienne > > > > > > > > > > > > > > -Sumit _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel