From patchwork Thu Mar 25 15:44:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 12164471 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.0 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,URIBL_BLOCKED, 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 DD948C433C1 for ; Thu, 25 Mar 2021 15:46:32 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 56A2C619EE for ; Thu, 25 Mar 2021 15:46:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 56A2C619EE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; 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=RZoAvJ03CGR91z9vSddIRJX2tmLk89pi9CAqhLdzO5k=; b=Ez9Fu5ExL7BfkbaiihqQeYr6B wS9Mvjc1S0WCwH+o/Q8fPKeNUarWj1yZPVGCH8gGZ3Z9sEArNv0yPcBUkE3ob0+zvqOehALDu1DR1 XjNFPqnKR7nVWuB/Zh6g1YAGPtsG6Pz7dCURbqXuetfRAFeM96cpZXOIz9Gngbl4I4vSYwWm7p3Zq JDeyw3XiP6ZoOCAiclbniPLKY4spV7eA1h1IIb4Fl9u/I7Yjiq/bl8zs30XW103VV000lFkhok2a5 TSwmt3u3z3SKH56LRK/6kjHcK+uostrkan8zBCwvYLE0DM14zXVIClqzoRoLN1pdCAuHdw2ZJ6ix/ IwZ1Z9rRQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lPSAR-001kUc-F9; Thu, 25 Mar 2021 15:45:04 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lPSA5-001kQa-C9 for linux-arm-kernel@lists.infradead.org; Thu, 25 Mar 2021 15:44:43 +0000 Received: by mail-lf1-x12a.google.com with SMTP id b14so3184998lfv.8 for ; Thu, 25 Mar 2021 08:44:38 -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=xCv397QUGgHoDE+Q2AV20/g8OqLkWLBfz3K3eMlY0VM=; b=XhAfOdZ8NHxWB287FSrtdPh/m9sUMQl7FX2x5uuSaMnzyWV/XNzj6cjZRrdX3yz0E7 26az/uO2fO7fgBdQ90VXdBhM4Zoh1CDvgzO/kEP5lTrhD712tmqPSReMd54G7vMOzDkk ZBtqGk8T2Ru3qPkZ9q/SRtECA8gPjhoNwObVFvYdioJjypdFNKn3clZ5XWwD+rbW0Hon DfU8WvLsfIospBrM/cl4TQA6sKUC/WYym0RIoEV3p1XVolV/L3D9pOah3FXnP/9OOdrO qmMNAVhuaVZoFhZui01fbNvrQjt6FiCgv3RChYOucXcYx20I48HwhSCXhImR/k5baI7s f4QA== 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=xCv397QUGgHoDE+Q2AV20/g8OqLkWLBfz3K3eMlY0VM=; b=dyLoGYHqYWiDhb/Q9t0vd5E0DmMQCh17ZxmlZbjtTCuFcT6TzTCKYPUlqGq28x9Vs1 esyIcuFcY0I0+MU1zJixeVOPxwTCj2JVFZ8krkP1jZXRXIG3XwPQTcOkHlrD0HVxF/CG arY1RPAdtWCcYN8BIpa/Xudul44pDf1rnYzLD8IRcZTcAb4F9LB0ML4UfNEWSDiDD/6f GBkX6XHtKj0itI6VXQfVZv5uIHSHRwMmj4ISXE2xVlzz/yk2fF+if3Z2mO5lpwN45Gdu f6i0dtM8IFola4NREv7o/V4eiMdLysim3NgHMsdCvpcF5UQZ9W0S1uscB10bhv8y7htY M95w== X-Gm-Message-State: AOAM533weu3elEwrfFyOCQrRmtPYqZCEymwNZ5KgtaFR9lkxEFNaYAjX 2UjPvU/+CIbIfjft+2/Ss7lTGg== X-Google-Smtp-Source: ABdhPJzyMpGMD7Wh2ePkDNY2Et2kYSedwMSEb2MVhczxmjf9lx2FmB8OAxAzKD0bQgbK/IbDPeNH1A== X-Received: by 2002:ac2:5a49:: with SMTP id r9mr5314036lfn.23.1616687077180; Thu, 25 Mar 2021 08:44:37 -0700 (PDT) Received: from jade.urgonet (h-249-223.A175.priv.bahnhof.se. [98.128.249.223]) by smtp.gmail.com with ESMTPSA id m27sm770916ljc.109.2021.03.25.08.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Mar 2021 08:44:36 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sudeep Holla , Marc Bonnici , Jerome Forissier , Jens Wiklander Subject: [PATCH 2/6] optee: simplify optee_release() Date: Thu, 25 Mar 2021 16:44:22 +0100 Message-Id: <20210325154426.3520148-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210325154426.3520148-1-jens.wiklander@linaro.org> References: <20210325154426.3520148-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-20210325_154441_552140_A0115749 X-CRM114-Status: GOOD ( 18.78 ) 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. Signed-off-by: Jens Wiklander --- drivers/tee/optee/call.c | 31 ++++++++++------- drivers/tee/optee/core.c | 55 +++++++++++-------------------- drivers/tee/optee/optee_private.h | 1 + 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index 780d7c4fd756..c0eca4d7d7f2 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -283,12 +283,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 */ @@ -301,16 +317,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 cf4718c6d35d..0ffe0c7d68ff 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -273,59 +273,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); - 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 = { @@ -349,7 +332,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 e25b216a14ef..2b63b796645e 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);