From patchwork Tue Aug 31 07:24:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 12466657 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A62F5C432BE for ; Tue, 31 Aug 2021 07:31:17 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 7217F60200 for ; Tue, 31 Aug 2021 07:31:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7217F60200 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=2jO/Y9FBx0jtVPSHj0WIrpID6txL/yu1Ub49SwygGZQ=; b=qA978r+V3POJhn YG+fWuIHcESlLvqWllk62zeVja9S8fJ2fJj7PeDoYhc3WjTTfpWLZopsnbgm2bGEUuK3QcOCFZvuf cZg9rJFqqS5YxgXZdx9fEGwSv7WaPbIlkNfT8lZkt1z4vTBd1oo4/ZK5clDMR8o4KiANikojXfzX+ UxkyflPK/knAQPOs9vgv3PxHkYeChcUMHr7JnwFAKMt2lYAJzobB3oWlPR0i2caayeEzUmoSEuJEg TIUSjiZwd+vp6LVHB3BEeN6Vm2eSuXYJMD+WNitLQGAfuI94BewBaAroKA+vD+kinKUWu9lnzQJS/ 445/HU4fF3q5nlkv6dhQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mKyCU-001eD6-JM; Tue, 31 Aug 2021 07:28:55 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mKy8r-001cUT-Bn for linux-arm-kernel@lists.infradead.org; Tue, 31 Aug 2021 07:25:16 +0000 Received: by mail-lj1-x232.google.com with SMTP id q21so30065504ljj.6 for ; Tue, 31 Aug 2021 00:25:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vBdWBM0UW4kI+oJm3DDC8ZI0wyWMqjtOzMb/smR/vC8=; b=ZnltFFLr1PEqPSFMshcpMIL0mUAmxXtNpgvLywKlYnTpPTxOpscUgc1lulIpWcBPsL KWf7t8oToMdUGNpsuhnpumpp7P8r+/vZmlydCOGPf9pofo6pcG5acjV5SrCac7zEI8TU 8nN77ep5UTqa8OyPJvCOZBY5FLV3xY9MWVD91mDBp3WcHJj5p4rzQV27DYKUWnbKuNdE fqkXUsvFYp5z48ePsgR8onYL3X1BfTpJEolVsKsnJqMMUmjWcFbFXzBVIVvOfRd4JIwN hJo0aR+uJbCxQtERU1sI3FZW+bHnGDXZap32ogYdkqjiqrcHbUKdTpG2Qd1Gi+I2iEl3 z6Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vBdWBM0UW4kI+oJm3DDC8ZI0wyWMqjtOzMb/smR/vC8=; b=KTiksFK1MCEntfsBgUC8C/1udfpGY1G/g0y1Ok5MpeAwiuJe9DiduojgiLeQvuqfOP s1sdYraDzSf5sbZcZAyiprqfIPTSN+nFYpuhTJ9QapWnDFEBK9wVhH/XyOPDpgTI5fBV ooOthat17IfxpI5lXjF6LNQZPoHdtFV2TkDzz+0hrrW++hTm7heN/onRzD/U/SgLqQ0V yBsN9nuydTOO3/oyMDhULbDAIczpASCa87BojSU/GwfXIbV1G6fP0yesEKdmXHh9Ihwh 9yA+Vhe22A5cuf6SGrGtOzk75HR3ezLWXZpA1c15ItdbzgYoQieDGN2ugie6p44z9uhb qXrg== X-Gm-Message-State: AOAM532STkmAnagERTmtvR7wg9VgVJyr+TsXxDig8DIBFb+nGPFHB4vz gz05PVv+khhSsEzdAK2Iq7Zz9ojpSWjKRw== X-Google-Smtp-Source: ABdhPJytTJ868aVldz1F29Jy24FweRp4vrEtoVPCLgtAoVZWQ5sXfeJYhR9kmL4aiV7v+Jvlp04zrg== X-Received: by 2002:a2e:bc1d:: with SMTP id b29mr25017364ljf.2.1630394707133; Tue, 31 Aug 2021 00:25:07 -0700 (PDT) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id c10sm1642569lfv.246.2021.08.31.00.25.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 00:25:06 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Sudeep Holla , Marc Bonnici , Jerome Forissier , sughosh.ganu@linaro.org, Jens Wiklander Subject: [PATCH v5 2/5] optee: simplify optee_release() Date: Tue, 31 Aug 2021 09:24:09 +0200 Message-Id: <20210831072412.887565-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210831072412.887565-1-jens.wiklander@linaro.org> References: <20210831072412.887565-1-jens.wiklander@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210831_002509_517698_613FB723 X-CRM114-Status: GOOD ( 19.21 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Simplifies optee_release() with a new helper function, optee_close_session_helper() which has been factored out from optee_close_session(). A separate optee_release_supp() is added for the supplicant device. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/call.c | 31 ++++++++++------- drivers/tee/optee/core.c | 56 +++++++++++-------------------- drivers/tee/optee/optee_private.h | 1 + 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index 945f03da0223..103976df2062 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -288,12 +288,28 @@ int optee_open_session(struct tee_context *ctx, return rc; } -int optee_close_session(struct tee_context *ctx, u32 session) +int optee_close_session_helper(struct tee_context *ctx, u32 session) { - struct optee_context_data *ctxdata = ctx->data; struct tee_shm *shm; struct optee_msg_arg *msg_arg; phys_addr_t msg_parg; + + shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg); + if (IS_ERR(shm)) + return PTR_ERR(shm); + + msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; + msg_arg->session = session; + optee_do_call_with_arg(ctx, msg_parg); + + tee_shm_free(shm); + + return 0; +} + +int optee_close_session(struct tee_context *ctx, u32 session) +{ + struct optee_context_data *ctxdata = ctx->data; struct optee_session *sess; /* Check that the session is valid and remove it from the list */ @@ -306,16 +322,7 @@ int optee_close_session(struct tee_context *ctx, u32 session) return -EINVAL; kfree(sess); - shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg); - if (IS_ERR(shm)) - return PTR_ERR(shm); - - msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; - msg_arg->session = session; - optee_do_call_with_arg(ctx, msg_parg); - - tee_shm_free(shm); - return 0; + return optee_close_session_helper(ctx, session); } int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index 5ce13b099d7d..86a1ae8d296d 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -264,60 +264,42 @@ static int optee_open(struct tee_context *ctx) return 0; } -static void optee_release(struct tee_context *ctx) +static void optee_release_helper(struct tee_context *ctx, + int (*close_session)(struct tee_context *ctx, + u32 session)) { struct optee_context_data *ctxdata = ctx->data; - struct tee_device *teedev = ctx->teedev; - struct optee *optee = tee_get_drvdata(teedev); - struct tee_shm *shm; - struct optee_msg_arg *arg = NULL; - phys_addr_t parg; struct optee_session *sess; struct optee_session *sess_tmp; if (!ctxdata) return; - shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), - TEE_SHM_MAPPED | TEE_SHM_PRIV); - if (!IS_ERR(shm)) { - arg = tee_shm_get_va(shm, 0); - /* - * If va2pa fails for some reason, we can't call into - * secure world, only free the memory. Secure OS will leak - * sessions and finally refuse more sessions, but we will - * at least let normal world reclaim its memory. - */ - if (!IS_ERR(arg)) - if (tee_shm_va2pa(shm, arg, &parg)) - arg = NULL; /* prevent usage of parg below */ - } - list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list, list_node) { list_del(&sess->list_node); - if (!IS_ERR_OR_NULL(arg)) { - memset(arg, 0, sizeof(*arg)); - arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; - arg->session = sess->session_id; - optee_do_call_with_arg(ctx, parg); - } + close_session(ctx, sess->session_id); kfree(sess); } kfree(ctxdata); + ctx->data = NULL; +} - if (!IS_ERR(shm)) - tee_shm_free(shm); +static void optee_release(struct tee_context *ctx) +{ + optee_release_helper(ctx, optee_close_session_helper); +} - ctx->data = NULL; +static void optee_release_supp(struct tee_context *ctx) +{ + struct optee *optee = tee_get_drvdata(ctx->teedev); - if (teedev == optee->supp_teedev) { - if (optee->scan_bus_wq) { - destroy_workqueue(optee->scan_bus_wq); - optee->scan_bus_wq = NULL; - } - optee_supp_release(&optee->supp); + optee_release_helper(ctx, optee_close_session_helper); + if (optee->scan_bus_wq) { + destroy_workqueue(optee->scan_bus_wq); + optee->scan_bus_wq = NULL; } + optee_supp_release(&optee->supp); } static const struct tee_driver_ops optee_ops = { @@ -341,7 +323,7 @@ static const struct tee_desc optee_desc = { static const struct tee_driver_ops optee_supp_ops = { .get_version = optee_get_version, .open = optee_open, - .release = optee_release, + .release = optee_release_supp, .supp_recv = optee_supp_recv, .supp_send = optee_supp_send, .shm_register = optee_shm_register_supp, diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h index dbdd367be156..d9f5f3f7ba58 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -152,6 +152,7 @@ u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg); int optee_open_session(struct tee_context *ctx, struct tee_ioctl_open_session_arg *arg, struct tee_param *param); +int optee_close_session_helper(struct tee_context *ctx, u32 session); int optee_close_session(struct tee_context *ctx, u32 session); int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, struct tee_param *param);