From patchwork Thu May 27 08:14:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 448838 Delivered-To: patches@linaro.org Received: by 2002:a02:7a1b:0:0:0:0:0 with SMTP id a27csp285426jac; Thu, 27 May 2021 01:14:20 -0700 (PDT) X-Received: by 2002:a19:e00f:: with SMTP id x15mr1551727lfg.222.1622103260037; Thu, 27 May 2021 01:14:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622103260; cv=none; d=google.com; s=arc-20160816; b=abI5O/hRXPoK4ii95lSdO8gnWOd+Ix1oiqi1n9fMELoz+A62j/XkIIguSEylNU/nwa ylqeEnnWpP5RCI8EpXTjA9L6UhyqgEdZ4Twm4dhYZoLxz94mzIQPwf9yBpDtrfvyHQxJ s71JdyqBfXCWeeffkHmHxceHVoCZN7iZwQqv87Mb/Qg3XmhN4KxltSEksTXcvJbNH4dm OxhUAVJAjoxod/jJlVIFZC6MrkTWq7lPVlvS3lb0sIuJyqw1jprDW3O1r4Ru4tSWyj+S +D+MUxjc5NynylV0r8g+htYNzSAD889leKz1AlqijQ+T9j+eVPjatxJEu7x8bSXxRjDE 6JfA== 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=CKkk0bkl/Y/YDtQR7mRIRJqKobsmFbjVwrk1oZtcPhw=; b=z30rSDfgAVRMRk2YNACDiltPZMySTU3PZPUfhWLJv130zB65nAe2Aae6Nb4t9jfSuR uZ7kR5027MaRv+UMcnDLppAPofHDXRRzArG0UXwzByYk6UcuaD63IBIh9ub0T3lP8vUD GC2OeHt2mUbOhzqJX1fN0QfNuJdUb0zs/x83iiH7Bec7yVaHyhpbtxVSF0TH1yYF3pH1 ZpEGflwfuhiaMPtw43SKwqwqLANUfsUZanvxFTn90UWgInaWvjo6ulOpoycq2UxMMFl5 4x0xTa6ID2CQyDZ7MmgSaqXk/0tEA5vDIF43D+jXXNcmpk08MWmOteL4fUk9HX1Oe6xw lrxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=vOwFC5vn; 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 b35sor315079lfv.172.2021.05.27.01.14.19 for (Google Transport Security); Thu, 27 May 2021 01:14:20 -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=vOwFC5vn; 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=CKkk0bkl/Y/YDtQR7mRIRJqKobsmFbjVwrk1oZtcPhw=; b=vOwFC5vnlEkQMRiEm9IzQei2xBI0SKjODb7wsRjIGyzTcGeJt5G4s6myuEixGNX1BS y2PxjYNcXDyOZY141yKYSctX7/veGtU/nHZoESfjuVwR8L0xuNddrrOnzrqlNuXPEOhC xlSjso4AXa1IOwkPnjgR1/gwDlV+SbJVdutDap84S4vbN2BXYZExIIQAcqoYAJxMxyHi eJ8xc9SBUtrmUgbABEbs8Ylf9zHD6hP+w+B6kpPx/PSzcfy3ANZswixZnhuEU+WY359E MOKZYSNvCfO79jIfjSEsSt/Whm4FvbOJy5ynBa00OYI1g87c2fxAmPDrjHCLG3hWz5uf vdxQ== 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=CKkk0bkl/Y/YDtQR7mRIRJqKobsmFbjVwrk1oZtcPhw=; b=V83u1pJXzC/2d+hgKpZmxQm/R26OPi+GKWbyB1UQrlzzowErpOuysVGMv7g6uj+0OM T3fRl+JG2y8Pbo3bem154tzZjVRT+Yd+BdmgmLwdA7qA3PuwiDpw+cGxSpvFdtgl/EdA pwKgLfHmM5G/p7ZucqkOufzPzNFrB9Pvulrap4VbyzL2eTHuLWA5gmQ3tTZQsO97Ivo7 Mc5M2NwWBXZlgKd5rVLkV0UFX1Jh3DsGwh7hl/g55WxnGfDmoDuDmFAxbysHiFLvlKQY XeuPpz6SmLaXVBDAqrlNlKsb3vZJJenUz+LFMUNJkqKlU4pT6XemPUudRFa/g1kdvzxD uIQA== X-Gm-Message-State: AOAM532Md1rQmZTDApwemldE77D+MNx4QeTkiglePyQZzBIsGQtPOBxm SZAWseUKZ3SEpLXkf4t311w/bNVI X-Google-Smtp-Source: ABdhPJw0QWoFBg5w1uIiO0JZknx9DZHK8XVuqraD+Q5aJOMiUb7trpj3hNn3LKZmhFhc9CeSK9nwnQ== X-Received: by 2002:a19:c18e:: with SMTP id r136mr1695399lff.266.1622103259657; Thu, 27 May 2021 01:14:19 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id z10sm133186lfe.228.2021.05.27.01.14.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 May 2021 01:14:19 -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 v2 2/5] optee: simplify optee_release() Date: Thu, 27 May 2021 10:14:01 +0200 Message-Id: <20210527081404.1433177-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210527081404.1433177-1-jens.wiklander@linaro.org> References: <20210527081404.1433177-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 6132cc8d014c..20092cf95c31 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -286,12 +286,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 */ @@ -304,16 +320,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 ddb8f9ecf307..949223b214c3 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -263,59 +263,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 = { @@ -339,7 +322,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);