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: 408831 Delivered-To: patches@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp647919jai; Thu, 25 Mar 2021 08:44:38 -0700 (PDT) X-Received: by 2002:ac2:5f0c:: with SMTP id 12mr5326237lfq.116.1616687077947; Thu, 25 Mar 2021 08:44:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616687077; cv=none; d=google.com; s=arc-20160816; b=hV1ha/7+blvBO0DI8JMkHoEgwV2LnG/Ip6TkX6EzUR0hhu3cSBTeOWv5M1R20RFjai 8Pq6buMKvLE3fAX9rdARrLAFU6w2CYosJzHyggY8NqK4flYYPH3kRjWFYwHyaHWyMU7R yU9PMHWuGaFyPg01FtSewrVsyYYVcLgdDJSiEyp36jxWqkoqj2hj5LbFU3zjlJlgvOpt ryUeKt2t2AThPOAnGJuJPC+jktkec28Kl0MYLS4x/3VVt5aVVzqYR//6YbAWHMUR6CYE mp9Ifa3HilBmzOo9mbOHTfw53K2mrn3BKPiCCCASxVBrxVnSi8H4jWTffVuAf7GJ94MC qjMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=xCv397QUGgHoDE+Q2AV20/g8OqLkWLBfz3K3eMlY0VM=; b=gue5sLQJPqZtdoK878Ryie01X/6pPF29ffSvcA/TZ19fuYmapNTLwM/65+6Lx1oTyK UCUkjhPasS8JqYx/kkw3xkYwyrdaFkhohpvgtnz/g+yA6j/pyF58zhzDn9sWN/dMRon3 1HSjQRxCEfdjGxiLG2SDbFKJ/5esxDk7u+dPa1xGxiFeuMK8D/1Ksgc7MY85RVIbTVFM rDFsMj8gW6vXkCwumR3BR7KsTMBv0A7jiVIhymY33EU0GXWi/jTDnV4RtSsNF9/hdz70 a81TjUKuJbZon9zyjJz3z6QVBAuEwWgl1WLGYNsN4b9VnGVcJno2WlGru9yCORyTRFhs RcQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XhAfOdZ8; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 63sor1508800lff.182.2021.03.25.08.44.37 for (Google Transport Security); Thu, 25 Mar 2021 08:44:37 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XhAfOdZ8; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=TEcN51EThswnI5EX8KXfjZfojHDHKp4hZpXyFbpp6rI49ZjArC7nYAg5+4UcyQOdRI Z87KVUV6SGp+bqaskQcFkrJX0uzbHUr3OAwlZHxQMhb/vIp5X0rlnYffdqGN3w16mTyq P2QBs7h97Hn72tsvbGYCe9IFvG3Vjf4mBqPY6KLg10gPPX1UJlnpuPf5Is63pSuHH9sT qSdbcIxZRs3OhAgLIuUgzWgn8+QErO/Qfb1HoeR4nk8j5talYHz1FDGusMHoTW8bJmcI mmEF/4QGPFWO79QwBcJ8DHqaLq/pw6U4YUkyCJbvhS5htmIlJkyjVhVvr17ygQOFVYFp fwqw== X-Gm-Message-State: AOAM532hsIaOluVErqFHrkV1ZsVVcXHpv66Ei08c8k9dD67k/FNr+2Cx yBkGaRN9dA8cve0dZKj3lI78noWJ0IDff/djo9k= 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) Return-Path: 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 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(-) -- 2.25.1 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);