From nobody Mon Jun 29 18:39:31 2026 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 C2E09C433EF for ; Fri, 4 Feb 2022 09:34:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349234AbiBDJeU (ORCPT ); Fri, 4 Feb 2022 04:34:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239431AbiBDJeM (ORCPT ); Fri, 4 Feb 2022 04:34:12 -0500 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 567D4C06173D for ; Fri, 4 Feb 2022 01:34:12 -0800 (PST) Received: by mail-lf1-x12e.google.com with SMTP id b9so11469564lfq.6 for ; Fri, 04 Feb 2022 01:34:12 -0800 (PST) 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=oN4lu8lbyhaxFZyXszWUPj3DflZg95OHRJxVYyEjsXM=; b=UU95mJTP6WlHYcPQ0GN9VROVMxSS0JRSVCzbKFEAYHnJoB4JyMBBEiEkiwvUuswO3Z SXFT3opI9McnKmzb5wMSV9LCGdlWtpJ0kzmItSwgLghRQ+Ym32W1nyVtOVYDh65Hcx0k APLu3aqR/ytZvJv3OtwGG8kcEDR5m1d8XXUziRFOP/3Q00qlxu1KCHkwVlyDbWWCcKEn kTAP1lO7yA+94HKU5PkuFhpfeQtd4JYnmYRNlTRK61gPxVzXR15l0HJMWoO2wyoQOeFd PyMIfw16cJW2PFXyJRcE45IgkxI959nl53NwzsTKIq1Lt4Kl33zO5zz77c/RcxQTctHK 65wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oN4lu8lbyhaxFZyXszWUPj3DflZg95OHRJxVYyEjsXM=; b=eRQNIP9xpcMdvkZoagUhuiB1ktO8U9Bu2G3ozsygNBH7BW77JzP1t+LysH11z9LFBX DNpuy4mwflSd2hQ2rcbsJ1FYa4M0HnPKX+ei0xJmR3YCZ4R9crAb1aQ6itx9I6D/ttsh fF+67R4IMxzwvDTQuxzRfCfdz5zD/S/qz+Z4FlSH9RsrCFJ/4St7iFn+ZwyIbPdqWZYq RhAhc7WyBsXV75IjoA38fHzYIEju2sPBn4HOVUGcglw0r+zK9TNQkEeusY2+qIqKG2sf LTLdb0MoF8iNZSuteeXMH/rLhbNV7ebWfEriDWsmiAcLUitodWG/uuM3WnrINcic3M6u 4jhw== X-Gm-Message-State: AOAM531Qwlo7JBJsY/6LlGhVP6+BRE0ibqx6bTh3eGA85wmyrNDfxjOZ Go25MTVRstv054i++P4nDOvj3yx0VeFxkQ== X-Google-Smtp-Source: ABdhPJziNpLJLMmIZ85Q4lpqOwiwZHjxS5EeMJG95tfsUjeRSr6gPUjZF/BbVRFly7nfdMPfiKSLJQ== X-Received: by 2002:ac2:54aa:: with SMTP id w10mr1691943lfk.357.1643967250387; Fri, 04 Feb 2022 01:34:10 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:09 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 01/10] hwrng: optee-rng: use tee_shm_alloc_kernel_buf() Date: Fri, 4 Feb 2022 10:33:50 +0100 Message-Id: <20220204093359.359059-2-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Uses the new simplified tee_shm_alloc_kernel_buf() function instead of the old deprecated tee_shm_alloc() function which required specific TEE_SHM-flags. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/char/hw_random/optee-rng.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/char/hw_random/optee-rng.c b/drivers/char/hw_random/op= tee-rng.c index 135a82590923..a948c0727b2b 100644 --- a/drivers/char/hw_random/optee-rng.c +++ b/drivers/char/hw_random/optee-rng.c @@ -145,10 +145,10 @@ static int optee_rng_init(struct hwrng *rng) struct optee_rng_private *pvt_data =3D to_optee_rng_private(rng); struct tee_shm *entropy_shm_pool =3D NULL; =20 - entropy_shm_pool =3D tee_shm_alloc(pvt_data->ctx, MAX_ENTROPY_REQ_SZ, - TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + entropy_shm_pool =3D tee_shm_alloc_kernel_buf(pvt_data->ctx, + MAX_ENTROPY_REQ_SZ); if (IS_ERR(entropy_shm_pool)) { - dev_err(pvt_data->dev, "tee_shm_alloc failed\n"); + dev_err(pvt_data->dev, "tee_shm_alloc_kernel_buf failed\n"); return PTR_ERR(entropy_shm_pool); } =20 --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 BB938C433F5 for ; Fri, 4 Feb 2022 09:34:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346685AbiBDJeR (ORCPT ); Fri, 4 Feb 2022 04:34:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239763AbiBDJeO (ORCPT ); Fri, 4 Feb 2022 04:34:14 -0500 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A06FC061401 for ; Fri, 4 Feb 2022 01:34:13 -0800 (PST) Received: by mail-lf1-x130.google.com with SMTP id z4so11508441lft.3 for ; Fri, 04 Feb 2022 01:34:13 -0800 (PST) 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=yn8zIxo8jyjO0ELBYvTuGmQzkeFiyQ40SLBI74VyhvY=; b=s8mMzmsSA1dE4oUddbq1qzNko2vK6jKtLdXrTc1toAkzT32q1UgpuZJv2X8EwvoO4w dBsTSDQBnPTt8XL5ySsufegkKwWtIwvMEmAlF7abJdOnfGVxb3sBkdaS4z+Za07vYJ/Z 5eCKgE6NqAk+UqYhX/2rOOeeIuSK63PFJRwHrKV5WGqu8biGmSlhGSwugJ+vq1nG1gy1 GpVGbRsX8+h8F0w5hsHD6NNUP97omkr4blgPyaR2xAMmKv67blb0dkMOj8Ikap4Gbtm8 FZ+krYmAxA/DlGSxWraANCrQpgSFpmZXtqCRqNIfJjaOIxYbLD/KvpyeyfSiCQvMlOso +ZDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yn8zIxo8jyjO0ELBYvTuGmQzkeFiyQ40SLBI74VyhvY=; b=JQj5OwxLVumLqEjpueKGHguqtseDlE6pY/Cs2LbgpEA/BWmnKPA4t/24TfMUvQG1+C SpQ0roJwfHdtb+gxFCccUeBEduK1vmqQQWFEL6opaNNDq8fz+mTqbszombldbxZYOH6Z 0l6zUE04KixAdh7OpbAh2BiSPlgNL/KIr9GkMZEypYQarkpclVMgpAcES+R22qS6Ch9Q YnaDGpRttim2W4noBbE6ol7orZ0sIHlS1K4fpWMJBkY1Ev1e6tn7l8lVJg0zBAy/odn9 iQYBIiYnr6AShCVnoS/eSNcvuiXN9Ujdu9oFwdSVZYOPV+057lnIF1mz2CynGBkKTopV u7xg== X-Gm-Message-State: AOAM531sOyn9xmAvwnzVz+g75cK5Gokp4gK2iJinrLdeOtG5Sr6nEvoH btG81H1QsE6rvi+qWBHgzlMYdv7Z/I5qJg== X-Google-Smtp-Source: ABdhPJwyoBH4bVmPRNuT8CDlgrmG2GDxZvniiD/FmgYX+eO2syxc7iCFkAGrQUt4L0Qdit9nEF5PBg== X-Received: by 2002:a05:6512:234f:: with SMTP id p15mr1775788lfu.606.1643967251531; Fri, 04 Feb 2022 01:34:11 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:11 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 02/10] tee: remove unused tee_shm_pool_alloc_res_mem() Date: Fri, 4 Feb 2022 10:33:51 +0100 Message-Id: <20220204093359.359059-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" None of the drivers in the TEE subsystem uses tee_shm_pool_alloc_res_mem() so remove the function. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_shm_pool.c | 56 -------------------------------------- include/linux/tee_drv.h | 30 -------------------- 2 files changed, 86 deletions(-) diff --git a/drivers/tee/tee_shm_pool.c b/drivers/tee/tee_shm_pool.c index fcbb461fc59c..a9f9d50fd181 100644 --- a/drivers/tee/tee_shm_pool.c +++ b/drivers/tee/tee_shm_pool.c @@ -47,62 +47,6 @@ static const struct tee_shm_pool_mgr_ops pool_ops_generi= c =3D { .destroy_poolmgr =3D pool_op_gen_destroy_poolmgr, }; =20 -/** - * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved - * memory range - * @priv_info: Information for driver private shared memory pool - * @dmabuf_info: Information for dma-buf shared memory pool - * - * Start and end of pools will must be page aligned. - * - * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied - * in @dmabuf, others will use the range provided by @priv. - * - * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. - */ -struct tee_shm_pool * -tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info, - struct tee_shm_pool_mem_info *dmabuf_info) -{ - struct tee_shm_pool_mgr *priv_mgr; - struct tee_shm_pool_mgr *dmabuf_mgr; - void *rc; - - /* - * Create the pool for driver private shared memory - */ - rc =3D tee_shm_pool_mgr_alloc_res_mem(priv_info->vaddr, priv_info->paddr, - priv_info->size, - 3 /* 8 byte aligned */); - if (IS_ERR(rc)) - return rc; - priv_mgr =3D rc; - - /* - * Create the pool for dma_buf shared memory - */ - rc =3D tee_shm_pool_mgr_alloc_res_mem(dmabuf_info->vaddr, - dmabuf_info->paddr, - dmabuf_info->size, PAGE_SHIFT); - if (IS_ERR(rc)) - goto err_free_priv_mgr; - dmabuf_mgr =3D rc; - - rc =3D tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); - if (IS_ERR(rc)) - goto err_free_dmabuf_mgr; - - return rc; - -err_free_dmabuf_mgr: - tee_shm_pool_mgr_destroy(dmabuf_mgr); -err_free_priv_mgr: - tee_shm_pool_mgr_destroy(priv_mgr); - - return rc; -} -EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem); - struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vadd= r, phys_addr_t paddr, size_t size, diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 5e1533ee3785..6b0f0d01ebdf 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -278,36 +278,6 @@ static inline void tee_shm_pool_mgr_destroy(struct tee= _shm_pool_mgr *poolm) poolm->ops->destroy_poolmgr(poolm); } =20 -/** - * struct tee_shm_pool_mem_info - holds information needed to create a sha= red - * memory pool - * @vaddr: Virtual address of start of pool - * @paddr: Physical address of start of pool - * @size: Size in bytes of the pool - */ -struct tee_shm_pool_mem_info { - unsigned long vaddr; - phys_addr_t paddr; - size_t size; -}; - -/** - * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved - * memory range - * @priv_info: Information for driver private shared memory pool - * @dmabuf_info: Information for dma-buf shared memory pool - * - * Start and end of pools will must be page aligned. - * - * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied - * in @dmabuf, others will use the range provided by @priv. - * - * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. - */ -struct tee_shm_pool * -tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info, - struct tee_shm_pool_mem_info *dmabuf_info); - /** * tee_shm_pool_free() - Free a shared memory pool * @pool: The shared memory pool to free --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 3D1FFC433EF for ; Fri, 4 Feb 2022 09:34:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356459AbiBDJeW (ORCPT ); Fri, 4 Feb 2022 04:34:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240258AbiBDJeO (ORCPT ); Fri, 4 Feb 2022 04:34:14 -0500 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DCD7C061744 for ; Fri, 4 Feb 2022 01:34:14 -0800 (PST) Received: by mail-lj1-x22a.google.com with SMTP id e17so7666958ljk.5 for ; Fri, 04 Feb 2022 01:34:14 -0800 (PST) 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=RJsD6xXO+PD8CsfT04affYv2wFE+MUYIEsBwIBL+qZs=; b=uS6Q6kvYh6eo56AILGB/zlaWJMz4UVzmSwmpN5n9ARa5o9bl/0pmeLCu47bX/gq0xI 5V/7PCfrRfmVPl4Xq3tn6fZtbfnMTEGo8l0ZpteSQPi/KNeUO9muKzzQORgSc1JOsDJ0 wjfK0z58z0NfGYUsold9uKXY3DvyAOiPwc/XEV4TsqlxIB97xUZlz6lbm8OVRNRmkgu5 aYKVtYxEii7xWYMD+GakzSsgMcN93SdKJeGNqnlbEIIPRF29jOmncOmwYPZ9bUfPTXnV O8pckVsNa9ScTopRjsFuDI0CIJP1K1pF8H0PDIXJGqe/Yr+T35dLn6DeCklGrIFSCsV5 aJaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RJsD6xXO+PD8CsfT04affYv2wFE+MUYIEsBwIBL+qZs=; b=fl8ExBRP8egJXFOsZKy69aYY2UD/c7JkYm/N0mshg+o8H2ceZVUW6aCU5V9PbSl58F i65HAIr+lj9eHhcPdlQewF9YEi/RjZ/bIe6rkUFC+k43qW697gVj4Va7hFz0Zo8jcJBr LSUbHBkJqG2SssEgMVbvI/I4yYhD2OL6oBt2EWN66/nFLQdtcD6a+lqzrMjCxHf3NRvM pQsSHHiETjWpz/2bj1MIGGxtOu3nDgwcto7Ya5qyb8y7xbzxuIlPUxLqJW/9qnMVUHw7 XrniMuwbhBfvMjlnYwb82Tlks1xAKU6JEWDx+C40kVSQocppoVBtzupP7bRJcRlErt5o BTWw== X-Gm-Message-State: AOAM533TVJzgJymboo7HELlzXUw+wG+0rFsL104EJkWBq9nniAUvvs1V 599zM7x0nlh3HlOGoixqgmq4gVPAl0FGgA== X-Google-Smtp-Source: ABdhPJwmi128R/blOX9fRTJYrg5u0YYPW66NQdgr+od2VQVLltUMJMRLX6Bs2g3riaQmEEGjakO6Lg== X-Received: by 2002:a2e:bd82:: with SMTP id o2mr1324075ljq.454.1643967252595; Fri, 04 Feb 2022 01:34:12 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:12 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 03/10] tee: add tee_shm_alloc_user_buf() Date: Fri, 4 Feb 2022 10:33:52 +0100 Message-Id: <20220204093359.359059-4-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adds a new function tee_shm_alloc_user_buf() for user mode allocations, replacing passing the flags TEE_SHM_MAPPED | TEE_SHM_DMA_BUF to tee_shm_alloc(). Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_core.c | 2 +- drivers/tee/tee_private.h | 2 ++ drivers/tee/tee_shm.c | 17 +++++++++++++++++ drivers/tee/tee_shm_pool.c | 2 +- include/linux/tee_drv.h | 2 +- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 3fc426dad2df..a15812baaeb1 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -297,7 +297,7 @@ static int tee_ioctl_shm_alloc(struct tee_context *ctx, if (data.flags) return -EINVAL; =20 - shm =3D tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + shm =3D tee_shm_alloc_user_buf(ctx, data.size); if (IS_ERR(shm)) return PTR_ERR(shm); =20 diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index e55204df31ce..e09c8aa5d967 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -68,4 +68,6 @@ void tee_device_put(struct tee_device *teedev); void teedev_ctx_get(struct tee_context *ctx); void teedev_ctx_put(struct tee_context *ctx); =20 +struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t siz= e); + #endif /*TEE_PRIVATE_H*/ diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 499fccba3d74..7e7e762fc1de 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -127,6 +127,23 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx,= size_t size, u32 flags) } EXPORT_SYMBOL_GPL(tee_shm_alloc); =20 +/** + * tee_shm_alloc_user_buf() - Allocate shared memory for user space + * @ctx: Context that allocates the shared memory + * @size: Requested size of shared memory + * + * Memory allocated as user space shared memory is automatically freed when + * the TEE file pointer is closed. The primary usage of this function is + * when the TEE driver doesn't support registering ordinary user space + * memory. + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t siz= e) +{ + return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); +} + /** * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer * @ctx: Context that allocates the shared memory diff --git a/drivers/tee/tee_shm_pool.c b/drivers/tee/tee_shm_pool.c index a9f9d50fd181..54c11aa374a8 100644 --- a/drivers/tee/tee_shm_pool.c +++ b/drivers/tee/tee_shm_pool.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * Copyright (c) 2015, Linaro Limited + * Copyright (c) 2015 Linaro Limited */ #include #include diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 6b0f0d01ebdf..a4393c8c38f3 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * Copyright (c) 2015-2016, Linaro Limited + * Copyright (c) 2015-2016 Linaro Limited */ =20 #ifndef __TEE_DRV_H --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 C0700C433FE for ; Fri, 4 Feb 2022 09:34:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357555AbiBDJe2 (ORCPT ); Fri, 4 Feb 2022 04:34:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243947AbiBDJeQ (ORCPT ); Fri, 4 Feb 2022 04:34:16 -0500 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28CF5C06173D for ; Fri, 4 Feb 2022 01:34:16 -0800 (PST) Received: by mail-lf1-x12a.google.com with SMTP id a28so11430385lfl.7 for ; Fri, 04 Feb 2022 01:34:16 -0800 (PST) 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=VhMkRjOuSPzE/m58f6lv55sbXHYgtqEEqfpd0/FUEGM=; b=ytMkUUhz+XMU4ht63MijKS8E6GMuWGxOfPG99XLZFycOPPAj7/b/UuX9Anex5xLXNo ki2vp3oA46ES+DI7MtDDIkFHnSvyv8leZtD4X88yKh9lFpyqk438AIw51fKxOi0e4Gav 7d9csjGZpPkwcVR7uNMBDN1T0swFdKIZqRHdxr3fx0xAAqDb1HGk1ShGHFGIzJRy0z+k G6Amo8O2dcmrJ/YjJ6ZNnRnxGqYv7PscrZsayDO6Glp4xz8ZlZl6st3zozP0HdO84H/x 1A4DGiiYzHmoc6M6zMr99jWjXhOoO76Hg4OXzdO9vHl+T5QyRAJ8bsrab6t16NKQp8Oi KoDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VhMkRjOuSPzE/m58f6lv55sbXHYgtqEEqfpd0/FUEGM=; b=1QArOOhAgp5wQLZu8tpJYOBd5x69Y/SznQbOsGxqB4C319q2OfpLAipslB6NcLVDLz WslzntCi3og0gVagPO32M7QoUMDkpdm5Ud9ZKFyV7nTC70raPTGhlbEYNGPOvH1Kjeg3 hUCjLzHFveWb12OEF5IdmbRa+4BTK8dg1Qyw+c6zodVzAVznjGQU7Pjpck0Y35Q7Fhss 6D9GAgObNs3b1PDINXboMzlx3SCpUJKE/HIYMBuq+HIbbZC7Kcq95QYXy1ZXXWeGLSzL H9w8+WdF9KhbKneI3Jx+icd6C7hVhJnXIvxpBRE/vQCjgperWeXWV8td96uVi3E8SAgN iW1A== X-Gm-Message-State: AOAM5333AVjjYnc+/F4f1MFwZus0v0XPpbGtKZ3M5pKqBX3b3mdsW5rC bnGVCANldZkvpE/Bgg82Fgi55WliZcnV8A== X-Google-Smtp-Source: ABdhPJwELZjz2ATNYReN7+pQhZrMfxcKGzVyhQJWHOHJMmOhSOZzWgLncLwGRkE3JVLrJNilQHLQhw== X-Received: by 2002:a05:6512:3e1a:: with SMTP id i26mr1677529lfv.175.1643967253747; Fri, 04 Feb 2022 01:34:13 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:13 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 04/10] tee: simplify shm pool handling Date: Fri, 4 Feb 2022 10:33:53 +0100 Message-Id: <20220204093359.359059-5-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Replaces the shared memory pool based on two pools with a single pool. The alloc() function pointer in struct tee_shm_pool_ops gets another parameter, align. This makes it possible to make less than page aligned allocations from the optional reserved shared memory pool while still making user space allocations page aligned. With in practice unchanged behaviour using only a single pool for bookkeeping. The allocation algorithm in the static OP-TEE shared memory pool is changed from best-fit to first-fit since only the latter supports an alignment parameter. The best-fit algorithm was previously the default choice and not a conscious one. The optee and amdtee drivers are updated as needed to work with this changed pool handling. This also removes OPTEE_SHM_NUM_PRIV_PAGES which becomes obsolete with this change as the private pages can be mixed with the payload pages. The OP-TEE driver changes minimum alignment for argument struct from 8 bytes to 512 bytes. A typical OP-TEE private shm allocation is 224 bytes (argument struct with 6 parameters, needed for open session). So with an alignment of 512 well waste a bit more than 50%. Before this we had a single page reserved for this so worst case usage compared to that would be 3 pages instead of 1 page. However, this worst case only occurs if there is a high pressure from multiple threads on secure world. All in all this should scale up and down better than fixed boundaries. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/amdtee/shm_pool.c | 55 +++++---------- drivers/tee/optee/Kconfig | 8 --- drivers/tee/optee/core.c | 11 +-- drivers/tee/optee/ffa_abi.c | 55 ++++----------- drivers/tee/optee/optee_private.h | 4 +- drivers/tee/optee/smc_abi.c | 108 +++++++++--------------------- drivers/tee/tee_private.h | 11 --- drivers/tee/tee_shm.c | 29 ++++---- drivers/tee/tee_shm_pool.c | 106 ++++++++++------------------- include/linux/tee_drv.h | 60 ++++++----------- 10 files changed, 137 insertions(+), 310 deletions(-) diff --git a/drivers/tee/amdtee/shm_pool.c b/drivers/tee/amdtee/shm_pool.c index 065854e2db18..f87f96a291c9 100644 --- a/drivers/tee/amdtee/shm_pool.c +++ b/drivers/tee/amdtee/shm_pool.c @@ -8,13 +8,17 @@ #include #include "amdtee_private.h" =20 -static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, struct tee_shm *s= hm, - size_t size) +static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align) { unsigned int order =3D get_order(size); unsigned long va; int rc; =20 + /* + * Ignore alignment since this is already going to be page aligned + * and there's no need for any larger alignment. + */ va =3D __get_free_pages(GFP_KERNEL | __GFP_ZERO, order); if (!va) return -ENOMEM; @@ -34,7 +38,7 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, = struct tee_shm *shm, return 0; } =20 -static void pool_op_free(struct tee_shm_pool_mgr *poolm, struct tee_shm *s= hm) +static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { /* Unmap the shared memory from TEE */ amdtee_unmap_shmem(shm); @@ -42,52 +46,25 @@ static void pool_op_free(struct tee_shm_pool_mgr *poolm= , struct tee_shm *shm) shm->kaddr =3D NULL; } =20 -static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm) +static void pool_op_destroy_pool(struct tee_shm_pool *pool) { - kfree(poolm); + kfree(pool); } =20 -static const struct tee_shm_pool_mgr_ops pool_ops =3D { +static const struct tee_shm_pool_ops pool_ops =3D { .alloc =3D pool_op_alloc, .free =3D pool_op_free, - .destroy_poolmgr =3D pool_op_destroy_poolmgr, + .destroy_pool =3D pool_op_destroy_pool, }; =20 -static struct tee_shm_pool_mgr *pool_mem_mgr_alloc(void) -{ - struct tee_shm_pool_mgr *mgr =3D kzalloc(sizeof(*mgr), GFP_KERNEL); - - if (!mgr) - return ERR_PTR(-ENOMEM); - - mgr->ops =3D &pool_ops; - - return mgr; -} - struct tee_shm_pool *amdtee_config_shm(void) { - struct tee_shm_pool_mgr *priv_mgr; - struct tee_shm_pool_mgr *dmabuf_mgr; - void *rc; + struct tee_shm_pool *pool =3D kzalloc(sizeof(*pool), GFP_KERNEL); =20 - rc =3D pool_mem_mgr_alloc(); - if (IS_ERR(rc)) - return rc; - priv_mgr =3D rc; - - rc =3D pool_mem_mgr_alloc(); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - return rc; - } - dmabuf_mgr =3D rc; + if (!pool) + return ERR_PTR(-ENOMEM); =20 - rc =3D tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - tee_shm_pool_mgr_destroy(dmabuf_mgr); - } + pool->ops =3D &pool_ops; =20 - return rc; + return pool; } diff --git a/drivers/tee/optee/Kconfig b/drivers/tee/optee/Kconfig index 3ca71e3812ed..f121c224e682 100644 --- a/drivers/tee/optee/Kconfig +++ b/drivers/tee/optee/Kconfig @@ -7,11 +7,3 @@ config OPTEE help This implements the OP-TEE Trusted Execution Environment (TEE) driver. - -config OPTEE_SHM_NUM_PRIV_PAGES - int "Private Shared Memory Pages" - default 1 - depends on OPTEE - help - This sets the number of private shared memory pages to be - used by OP-TEE TEE driver. diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index 17a6f51d3089..f4bccb5f0e93 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -18,8 +18,8 @@ #include #include "optee_private.h" =20 -int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size, +int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *= shm, + size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, struct page **pages, @@ -30,6 +30,10 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *= poolm, struct page *page; int rc =3D 0; =20 + /* + * Ignore alignment since this is already going to be page aligned + * and there's no need for any larger alignment. + */ page =3D alloc_pages(GFP_KERNEL | __GFP_ZERO, order); if (!page) return -ENOMEM; @@ -51,7 +55,6 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *p= oolm, for (i =3D 0; i < nr_pages; i++) pages[i] =3D page + i; =20 - shm->flags |=3D TEE_SHM_REGISTER; rc =3D shm_register(shm->ctx, shm, pages, nr_pages, (unsigned long)shm->kaddr); kfree(pages); @@ -62,7 +65,7 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *p= oolm, return 0; =20 err: - __free_pages(page, order); + free_pages((unsigned long)shm->kaddr, order); return rc; } =20 diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index f2bf6c61197f..b9b898b124d3 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -369,14 +369,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_c= ontext *ctx, * The main function is optee_ffa_shm_pool_alloc_pages(). */ =20 -static int pool_ffa_op_alloc(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size) +static int pool_ffa_op_alloc(struct tee_shm_pool *pool, + struct tee_shm *shm, size_t size, size_t align) { - return optee_pool_op_alloc_helper(poolm, shm, size, + return optee_pool_op_alloc_helper(pool, shm, size, align, optee_ffa_shm_register); } =20 -static void pool_ffa_op_free(struct tee_shm_pool_mgr *poolm, +static void pool_ffa_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { optee_ffa_shm_unregister(shm->ctx, shm); @@ -384,15 +384,15 @@ static void pool_ffa_op_free(struct tee_shm_pool_mgr = *poolm, shm->kaddr =3D NULL; } =20 -static void pool_ffa_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm) +static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool) { - kfree(poolm); + kfree(pool); } =20 -static const struct tee_shm_pool_mgr_ops pool_ffa_ops =3D { +static const struct tee_shm_pool_ops pool_ffa_ops =3D { .alloc =3D pool_ffa_op_alloc, .free =3D pool_ffa_op_free, - .destroy_poolmgr =3D pool_ffa_op_destroy_poolmgr, + .destroy_pool =3D pool_ffa_op_destroy_pool, }; =20 /** @@ -401,16 +401,16 @@ static const struct tee_shm_pool_mgr_ops pool_ffa_ops= =3D { * This pool is used with OP-TEE over FF-A. In this case command buffers * and such are allocated from kernel's own memory. */ -static struct tee_shm_pool_mgr *optee_ffa_shm_pool_alloc_pages(void) +static struct tee_shm_pool *optee_ffa_shm_pool_alloc_pages(void) { - struct tee_shm_pool_mgr *mgr =3D kzalloc(sizeof(*mgr), GFP_KERNEL); + struct tee_shm_pool *pool =3D kzalloc(sizeof(*pool), GFP_KERNEL); =20 - if (!mgr) + if (!pool) return ERR_PTR(-ENOMEM); =20 - mgr->ops =3D &pool_ffa_ops; + pool->ops =3D &pool_ffa_ops; =20 - return mgr; + return pool; } =20 /* @@ -700,33 +700,6 @@ static bool optee_ffa_exchange_caps(struct ffa_device = *ffa_dev, return true; } =20 -static struct tee_shm_pool *optee_ffa_config_dyn_shm(void) -{ - struct tee_shm_pool_mgr *priv_mgr; - struct tee_shm_pool_mgr *dmabuf_mgr; - void *rc; - - rc =3D optee_ffa_shm_pool_alloc_pages(); - if (IS_ERR(rc)) - return rc; - priv_mgr =3D rc; - - rc =3D optee_ffa_shm_pool_alloc_pages(); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - return rc; - } - dmabuf_mgr =3D rc; - - rc =3D tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - tee_shm_pool_mgr_destroy(dmabuf_mgr); - } - - return rc; -} - static void optee_ffa_get_version(struct tee_device *teedev, struct tee_ioctl_version_data *vers) { @@ -824,7 +797,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) if (!optee) return -ENOMEM; =20 - pool =3D optee_ffa_config_dyn_shm(); + pool =3D optee_ffa_shm_pool_alloc_pages(); if (IS_ERR(pool)) { rc =3D PTR_ERR(pool); goto err_free_optee; diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index 92bc47bef95f..df3a483bbf46 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -229,8 +229,8 @@ int optee_cancel_req(struct tee_context *ctx, u32 cance= l_id, u32 session); int optee_enumerate_devices(u32 func); void optee_unregister_devices(void); =20 -int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size, +int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *= shm, + size_t size, size_t align, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, struct page **pages, diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 1a55339c7072..abb360fd5a5c 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -42,7 +42,15 @@ * 6. Driver initialization. */ =20 -#define OPTEE_SHM_NUM_PRIV_PAGES CONFIG_OPTEE_SHM_NUM_PRIV_PAGES +/* + * A typical OP-TEE private shm allocation is 224 bytes (argument struct + * with 6 parameters, needed for open session). So with an alignment of 512 + * we'll waste a bit more than 50%. However, it's only expected that we'll + * have a handful of these structs allocated at a time. Most memory will + * be allocated aligned to the page size, So all in all this should scale + * up and down quite well. + */ +#define OPTEE_MIN_STATIC_POOL_ALIGN 9 /* 512 bytes aligned */ =20 /* * 1. Convert between struct tee_param and struct optee_msg_param @@ -522,20 +530,21 @@ static int optee_shm_unregister_supp(struct tee_conte= xt *ctx, * The main function is optee_shm_pool_alloc_pages(). */ =20 -static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size) +static int pool_op_alloc(struct tee_shm_pool *pool, + struct tee_shm *shm, size_t size, size_t align) { /* * Shared memory private to the OP-TEE driver doesn't need * to be registered with OP-TEE. */ if (shm->flags & TEE_SHM_PRIV) - return optee_pool_op_alloc_helper(poolm, shm, size, NULL); + return optee_pool_op_alloc_helper(pool, shm, size, align, NULL); =20 - return optee_pool_op_alloc_helper(poolm, shm, size, optee_shm_register); + return optee_pool_op_alloc_helper(pool, shm, size, align, + optee_shm_register); } =20 -static void pool_op_free(struct tee_shm_pool_mgr *poolm, +static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { if (!(shm->flags & TEE_SHM_PRIV)) @@ -545,15 +554,15 @@ static void pool_op_free(struct tee_shm_pool_mgr *poo= lm, shm->kaddr =3D NULL; } =20 -static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm) +static void pool_op_destroy_pool(struct tee_shm_pool *pool) { - kfree(poolm); + kfree(pool); } =20 -static const struct tee_shm_pool_mgr_ops pool_ops =3D { +static const struct tee_shm_pool_ops pool_ops =3D { .alloc =3D pool_op_alloc, .free =3D pool_op_free, - .destroy_poolmgr =3D pool_op_destroy_poolmgr, + .destroy_pool =3D pool_op_destroy_pool, }; =20 /** @@ -562,16 +571,16 @@ static const struct tee_shm_pool_mgr_ops pool_ops =3D= { * This pool is used when OP-TEE supports dymanic SHM. In this case * command buffers and such are allocated from kernel's own memory. */ -static struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void) +static struct tee_shm_pool *optee_shm_pool_alloc_pages(void) { - struct tee_shm_pool_mgr *mgr =3D kzalloc(sizeof(*mgr), GFP_KERNEL); + struct tee_shm_pool *pool =3D kzalloc(sizeof(*pool), GFP_KERNEL); =20 - if (!mgr) + if (!pool) return ERR_PTR(-ENOMEM); =20 - mgr->ops =3D &pool_ops; + pool->ops =3D &pool_ops; =20 - return mgr; + return pool; } =20 /* @@ -1143,33 +1152,6 @@ static bool optee_msg_exchange_capabilities(optee_in= voke_fn *invoke_fn, return true; } =20 -static struct tee_shm_pool *optee_config_dyn_shm(void) -{ - struct tee_shm_pool_mgr *priv_mgr; - struct tee_shm_pool_mgr *dmabuf_mgr; - void *rc; - - rc =3D optee_shm_pool_alloc_pages(); - if (IS_ERR(rc)) - return rc; - priv_mgr =3D rc; - - rc =3D optee_shm_pool_alloc_pages(); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - return rc; - } - dmabuf_mgr =3D rc; - - rc =3D tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - tee_shm_pool_mgr_destroy(dmabuf_mgr); - } - - return rc; -} - static struct tee_shm_pool * optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_sh= m) { @@ -1183,10 +1165,7 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn= , void **memremaped_shm) phys_addr_t begin; phys_addr_t end; void *va; - struct tee_shm_pool_mgr *priv_mgr; - struct tee_shm_pool_mgr *dmabuf_mgr; void *rc; - const int sz =3D OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE; =20 invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc); if (res.result.status !=3D OPTEE_SMC_RETURN_OK) { @@ -1204,11 +1183,6 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn= , void **memremaped_shm) paddr =3D begin; size =3D end - begin; =20 - if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) { - pr_err("too small shared memory area\n"); - return ERR_PTR(-EINVAL); - } - va =3D memremap(paddr, size, MEMREMAP_WB); if (!va) { pr_err("shared memory ioremap failed\n"); @@ -1216,35 +1190,13 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_f= n, void **memremaped_shm) } vaddr =3D (unsigned long)va; =20 - rc =3D tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz, - 3 /* 8 bytes aligned */); - if (IS_ERR(rc)) - goto err_memunmap; - priv_mgr =3D rc; - - vaddr +=3D sz; - paddr +=3D sz; - size -=3D sz; - - rc =3D tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT); - if (IS_ERR(rc)) - goto err_free_priv_mgr; - dmabuf_mgr =3D rc; - - rc =3D tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); + rc =3D tee_shm_pool_alloc_res_mem(vaddr, paddr, size, + OPTEE_MIN_STATIC_POOL_ALIGN); if (IS_ERR(rc)) - goto err_free_dmabuf_mgr; - - *memremaped_shm =3D va; - - return rc; + memunmap(va); + else + *memremaped_shm =3D va; =20 -err_free_dmabuf_mgr: - tee_shm_pool_mgr_destroy(dmabuf_mgr); -err_free_priv_mgr: - tee_shm_pool_mgr_destroy(priv_mgr); -err_memunmap: - memunmap(va); return rc; } =20 @@ -1366,7 +1318,7 @@ static int optee_probe(struct platform_device *pdev) * Try to use dynamic shared memory if possible */ if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) - pool =3D optee_config_dyn_shm(); + pool =3D optee_shm_pool_alloc_pages(); =20 /* * If dynamic shared memory is not available or failed - try static one diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index e09c8aa5d967..7265f47c6d8e 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -12,17 +12,6 @@ #include #include =20 -/** - * struct tee_shm_pool - shared memory pool - * @private_mgr: pool manager for shared memory only between kernel - * and secure world - * @dma_buf_mgr: pool manager for shared memory exported to user space - */ -struct tee_shm_pool { - struct tee_shm_pool_mgr *private_mgr; - struct tee_shm_pool_mgr *dma_buf_mgr; -}; - #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 =20 diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 7e7e762fc1de..f0a9cccd2f2c 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -31,14 +31,7 @@ static void release_registered_pages(struct tee_shm *shm) static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) { if (shm->flags & TEE_SHM_POOL) { - struct tee_shm_pool_mgr *poolm; - - if (shm->flags & TEE_SHM_DMA_BUF) - poolm =3D teedev->pool->dma_buf_mgr; - else - poolm =3D teedev->pool->private_mgr; - - poolm->ops->free(poolm, shm); + teedev->pool->ops->free(teedev->pool, shm); } else if (shm->flags & TEE_SHM_REGISTER) { int rc =3D teedev->desc->ops->shm_unregister(shm->ctx, shm); =20 @@ -59,8 +52,8 @@ static void tee_shm_release(struct tee_device *teedev, st= ruct tee_shm *shm) struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 fl= ags) { struct tee_device *teedev =3D ctx->teedev; - struct tee_shm_pool_mgr *poolm =3D NULL; struct tee_shm *shm; + size_t align; void *ret; int rc; =20 @@ -93,12 +86,18 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, = size_t size, u32 flags) refcount_set(&shm->refcount, 1); shm->flags =3D flags | TEE_SHM_POOL; shm->ctx =3D ctx; - if (flags & TEE_SHM_DMA_BUF) - poolm =3D teedev->pool->dma_buf_mgr; - else - poolm =3D teedev->pool->private_mgr; + if (flags & TEE_SHM_DMA_BUF) { + align =3D PAGE_SIZE; + /* + * Request to register the shm in the pool allocator below + * if supported. + */ + shm->flags |=3D TEE_SHM_REGISTER; + } else { + align =3D 2 * sizeof(long); + } =20 - rc =3D poolm->ops->alloc(poolm, shm, size); + rc =3D teedev->pool->ops->alloc(teedev->pool, shm, size, align); if (rc) { ret =3D ERR_PTR(rc); goto err_kfree; @@ -118,7 +117,7 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, = size_t size, u32 flags) =20 return shm; err_pool_free: - poolm->ops->free(poolm, shm); + teedev->pool->ops->free(teedev->pool, shm); err_kfree: kfree(shm); err_dev_put: diff --git a/drivers/tee/tee_shm_pool.c b/drivers/tee/tee_shm_pool.c index 54c11aa374a8..71e0f8ae69aa 100644 --- a/drivers/tee/tee_shm_pool.c +++ b/drivers/tee/tee_shm_pool.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * Copyright (c) 2015 Linaro Limited + * Copyright (c) 2015, 2017, 2022 Linaro Limited */ #include #include @@ -9,14 +9,16 @@ #include #include "tee_private.h" =20 -static int pool_op_gen_alloc(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm, size_t size) +static int pool_op_gen_alloc(struct tee_shm_pool *pool, struct tee_shm *sh= m, + size_t size, size_t align) { unsigned long va; - struct gen_pool *genpool =3D poolm->private_data; - size_t s =3D roundup(size, 1 << genpool->min_alloc_order); + struct gen_pool *genpool =3D pool->private_data; + size_t a =3D max_t(size_t, align, BIT(genpool->min_alloc_order)); + struct genpool_data_align data =3D { .align =3D a }; + size_t s =3D roundup(size, a); =20 - va =3D gen_pool_alloc(genpool, s); + va =3D gen_pool_alloc_algo(genpool, s, gen_pool_first_fit_align, &data); if (!va) return -ENOMEM; =20 @@ -24,107 +26,67 @@ static int pool_op_gen_alloc(struct tee_shm_pool_mgr *= poolm, shm->kaddr =3D (void *)va; shm->paddr =3D gen_pool_virt_to_phys(genpool, va); shm->size =3D s; + /* + * This is from a static shared memory pool so no need to register + * each chunk, and no need to unregister later either. + */ + shm->flags &=3D ~TEE_SHM_REGISTER; return 0; } =20 -static void pool_op_gen_free(struct tee_shm_pool_mgr *poolm, - struct tee_shm *shm) +static void pool_op_gen_free(struct tee_shm_pool *pool, struct tee_shm *sh= m) { - gen_pool_free(poolm->private_data, (unsigned long)shm->kaddr, + gen_pool_free(pool->private_data, (unsigned long)shm->kaddr, shm->size); shm->kaddr =3D NULL; } =20 -static void pool_op_gen_destroy_poolmgr(struct tee_shm_pool_mgr *poolm) +static void pool_op_gen_destroy_pool(struct tee_shm_pool *pool) { - gen_pool_destroy(poolm->private_data); - kfree(poolm); + gen_pool_destroy(pool->private_data); + kfree(pool); } =20 -static const struct tee_shm_pool_mgr_ops pool_ops_generic =3D { +static const struct tee_shm_pool_ops pool_ops_generic =3D { .alloc =3D pool_op_gen_alloc, .free =3D pool_op_gen_free, - .destroy_poolmgr =3D pool_op_gen_destroy_poolmgr, + .destroy_pool =3D pool_op_gen_destroy_pool, }; =20 -struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vadd= r, - phys_addr_t paddr, - size_t size, - int min_alloc_order) +struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr, + phys_addr_t paddr, size_t size, + int min_alloc_order) { const size_t page_mask =3D PAGE_SIZE - 1; - struct tee_shm_pool_mgr *mgr; + struct tee_shm_pool *pool; int rc; =20 /* Start and end must be page aligned */ if (vaddr & page_mask || paddr & page_mask || size & page_mask) return ERR_PTR(-EINVAL); =20 - mgr =3D kzalloc(sizeof(*mgr), GFP_KERNEL); - if (!mgr) + pool =3D kzalloc(sizeof(*pool), GFP_KERNEL); + if (!pool) return ERR_PTR(-ENOMEM); =20 - mgr->private_data =3D gen_pool_create(min_alloc_order, -1); - if (!mgr->private_data) { + pool->private_data =3D gen_pool_create(min_alloc_order, -1); + if (!pool->private_data) { rc =3D -ENOMEM; goto err; } =20 - gen_pool_set_algo(mgr->private_data, gen_pool_best_fit, NULL); - rc =3D gen_pool_add_virt(mgr->private_data, vaddr, paddr, size, -1); + rc =3D gen_pool_add_virt(pool->private_data, vaddr, paddr, size, -1); if (rc) { - gen_pool_destroy(mgr->private_data); + gen_pool_destroy(pool->private_data); goto err; } =20 - mgr->ops =3D &pool_ops_generic; + pool->ops =3D &pool_ops_generic; =20 - return mgr; + return pool; err: - kfree(mgr); + kfree(pool); =20 return ERR_PTR(rc); } -EXPORT_SYMBOL_GPL(tee_shm_pool_mgr_alloc_res_mem); - -static bool check_mgr_ops(struct tee_shm_pool_mgr *mgr) -{ - return mgr && mgr->ops && mgr->ops->alloc && mgr->ops->free && - mgr->ops->destroy_poolmgr; -} - -struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr, - struct tee_shm_pool_mgr *dmabuf_mgr) -{ - struct tee_shm_pool *pool; - - if (!check_mgr_ops(priv_mgr) || !check_mgr_ops(dmabuf_mgr)) - return ERR_PTR(-EINVAL); - - pool =3D kzalloc(sizeof(*pool), GFP_KERNEL); - if (!pool) - return ERR_PTR(-ENOMEM); - - pool->private_mgr =3D priv_mgr; - pool->dma_buf_mgr =3D dmabuf_mgr; - - return pool; -} -EXPORT_SYMBOL_GPL(tee_shm_pool_alloc); - -/** - * tee_shm_pool_free() - Free a shared memory pool - * @pool: The shared memory pool to free - * - * There must be no remaining shared memory allocated from this pool when - * this function is called. - */ -void tee_shm_pool_free(struct tee_shm_pool *pool) -{ - if (pool->private_mgr) - tee_shm_pool_mgr_destroy(pool->private_mgr); - if (pool->dma_buf_mgr) - tee_shm_pool_mgr_destroy(pool->dma_buf_mgr); - kfree(pool); -} -EXPORT_SYMBOL_GPL(tee_shm_pool_free); +EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem); diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index a4393c8c38f3..ed641dc314bd 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * Copyright (c) 2015-2016 Linaro Limited + * Copyright (c) 2015-2022 Linaro Limited */ =20 #ifndef __TEE_DRV_H @@ -221,62 +221,39 @@ struct tee_shm { }; =20 /** - * struct tee_shm_pool_mgr - shared memory manager + * struct tee_shm_pool - shared memory pool * @ops: operations * @private_data: private data for the shared memory manager */ -struct tee_shm_pool_mgr { - const struct tee_shm_pool_mgr_ops *ops; +struct tee_shm_pool { + const struct tee_shm_pool_ops *ops; void *private_data; }; =20 /** - * struct tee_shm_pool_mgr_ops - shared memory pool manager operations + * struct tee_shm_pool_ops - shared memory pool operations * @alloc: called when allocating shared memory * @free: called when freeing shared memory - * @destroy_poolmgr: called when destroying the pool manager + * @destroy_pool: called when destroying the pool */ -struct tee_shm_pool_mgr_ops { - int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm, - size_t size); - void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm); - void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr); +struct tee_shm_pool_ops { + int (*alloc)(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align); + void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm); + void (*destroy_pool)(struct tee_shm_pool *pool); }; =20 -/** - * tee_shm_pool_alloc() - Create a shared memory pool from shm managers - * @priv_mgr: manager for driver private shared memory allocations - * @dmabuf_mgr: manager for dma-buf shared memory allocations - * - * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied - * in @dmabuf, others will use the range provided by @priv. - * - * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. - */ -struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr, - struct tee_shm_pool_mgr *dmabuf_mgr); - /* - * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved - * memory + * tee_shm_pool_alloc_res_mem() - Create a shm manager for reserved memory * @vaddr: Virtual address of start of pool * @paddr: Physical address of start of pool * @size: Size in bytes of the pool * - * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failur= e. - */ -struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vadd= r, - phys_addr_t paddr, - size_t size, - int min_alloc_order); - -/** - * tee_shm_pool_mgr_destroy() - Free a shared memory manager + * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. */ -static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm) -{ - poolm->ops->destroy_poolmgr(poolm); -} +struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr, + phys_addr_t paddr, size_t size, + int min_alloc_order); =20 /** * tee_shm_pool_free() - Free a shared memory pool @@ -285,7 +262,10 @@ static inline void tee_shm_pool_mgr_destroy(struct tee= _shm_pool_mgr *poolm) * The must be no remaining shared memory allocated from this pool when * this function is called. */ -void tee_shm_pool_free(struct tee_shm_pool *pool); +static inline void tee_shm_pool_free(struct tee_shm_pool *pool) +{ + pool->ops->destroy_pool(pool); +} =20 /** * tee_get_drvdata() - Return driver_data pointer --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 9B8A1C433EF for ; Fri, 4 Feb 2022 09:34:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357531AbiBDJe0 (ORCPT ); Fri, 4 Feb 2022 04:34:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345520AbiBDJeR (ORCPT ); Fri, 4 Feb 2022 04:34:17 -0500 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08189C061401 for ; Fri, 4 Feb 2022 01:34:17 -0800 (PST) Received: by mail-lf1-x12d.google.com with SMTP id u14so11394643lfo.11 for ; Fri, 04 Feb 2022 01:34:16 -0800 (PST) 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=9bbBa8iaPsUWfadVcmuE6wOiniwihoElxDdQPBTkB5c=; b=SUNasxzjit459LHMbN7jyQO946qOJG/PYtlEzNO11M0cbGtPC7up6xhDWbmrA3JgVt UAMPmXjZ1wqNIoZbVpAgwCD8adzaboECDHrKAQPLCCsRyacodgCRXFN1B0LomUNitKUz aakDMa1Ip9DiXg+BlCS9zA6SjqPYmgX0tr7qqsI9bDl0HQpw6iFt+XrckzNSDR4FhU4l GQnTfrHEH21qwqdaqHY9M0WPUsNMXuDpj5QqN3q7HOaHkd81tMwLqXTt1AvJKwFvyqJS Pswkb9zfcQG4z/pyjAxJKgEQs20jF6VK3Nm/bxpU5krAUlvKa5/XAL7El7tjR5C77cH0 wqKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9bbBa8iaPsUWfadVcmuE6wOiniwihoElxDdQPBTkB5c=; b=wKaVABmfiHG5QXR+SnGgAqUGtkEkwprn1ZCicOCbXX3uJ1V0HpoIZRZf2u4rQVChbi 60isPXp8ec8IaZ3CDKYm7Hg9k1ZrHRpRADQSDXosscPlFklcAhcQ+2ZQjCT5fxXWEiX6 R6dF5A7w9dUSNECqazd14J6XKOCKbrcEjZqntQaN/i7pJyF+hiulQnomelbIVFwmgf7e NXLOwz11yMw1e959IG1eeM9u4NZkrx1f2cIKasZxZYUHvhIqruZdZdVVYPs2P5JQiV55 Zw/UvhXPH34ALQgARMell8Nbus35OaYp5UCBwhlH04l/RUOpLsFjhFQG9hGZ1Em82UKJ x+MA== X-Gm-Message-State: AOAM531Nc9AGpJJQB41gEaAbmKjiGdUMB+dwhrgs5uHoMAMnLk8ceBdM leR6vaDnPHQNZk6kIVc2CuLzNC5TBlNgfA== X-Google-Smtp-Source: ABdhPJwvvTc81qluJq7fa6yC9XS8zeU4+2MpI17UGSdZQanAD0bYPK5uT3L3aqY5mmvt4HAePmZxNA== X-Received: by 2002:a05:6512:3184:: with SMTP id i4mr1703201lfe.293.1643967255000; Fri, 04 Feb 2022 01:34:15 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:14 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 05/10] tee: replace tee_shm_alloc() Date: Fri, 4 Feb 2022 10:33:54 +0100 Message-Id: <20220204093359.359059-6-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" tee_shm_alloc() is replaced by three new functions, tee_shm_alloc_user_buf() - for user mode allocations, replacing passing the flags TEE_SHM_MAPPED | TEE_SHM_DMA_BUF tee_shm_alloc_kernel_buf() - for kernel mode allocations, slightly optimized compared to using the flags TEE_SHM_MAPPED | TEE_SHM_DMA_BUF. tee_shm_alloc_priv_buf() - primarily for TEE driver internal use. This also makes the interface easier to use as we can get rid of the somewhat hard to use flags parameter. The TEE subsystem and the TEE drivers are updated to use the new functions instead. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/call.c | 2 +- drivers/tee/optee/device.c | 5 +- drivers/tee/optee/ffa_abi.c | 4 +- drivers/tee/optee/smc_abi.c | 6 +- drivers/tee/tee_shm.c | 108 +++++++++++++++++++++++------------- include/linux/tee_drv.h | 16 +----- 6 files changed, 76 insertions(+), 65 deletions(-) diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index b25cc1fac945..bd49ec934060 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -120,7 +120,7 @@ struct tee_shm *optee_get_msg_arg(struct tee_context *c= tx, size_t num_params, if (optee->rpc_arg_count) sz +=3D OPTEE_MSG_GET_ARG_SIZE(optee->rpc_arg_count); =20 - shm =3D tee_shm_alloc(ctx, sz, TEE_SHM_MAPPED | TEE_SHM_PRIV); + shm =3D tee_shm_alloc_priv_buf(ctx, sz); if (IS_ERR(shm)) return shm; =20 diff --git a/drivers/tee/optee/device.c b/drivers/tee/optee/device.c index 128a2d2a50a1..f3947be13e2e 100644 --- a/drivers/tee/optee/device.c +++ b/drivers/tee/optee/device.c @@ -121,10 +121,9 @@ static int __optee_enumerate_devices(u32 func) if (rc < 0 || !shm_size) goto out_sess; =20 - device_shm =3D tee_shm_alloc(ctx, shm_size, - TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + device_shm =3D tee_shm_alloc_kernel_buf(ctx, shm_size); if (IS_ERR(device_shm)) { - pr_err("tee_shm_alloc failed\n"); + pr_err("tee_shm_alloc_kernel_buf failed\n"); rc =3D PTR_ERR(device_shm); goto out_sess; } diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index b9b898b124d3..bf85975c6e95 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -440,8 +440,8 @@ static void handle_ffa_rpc_func_cmd_shm_alloc(struct te= e_context *ctx, shm =3D optee_rpc_cmd_alloc_suppl(ctx, arg->params[0].u.value.b); break; case OPTEE_RPC_SHM_TYPE_KERNEL: - shm =3D tee_shm_alloc(optee->ctx, arg->params[0].u.value.b, - TEE_SHM_MAPPED | TEE_SHM_PRIV); + shm =3D tee_shm_alloc_priv_buf(optee->ctx, + arg->params[0].u.value.b); break; default: arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index abb360fd5a5c..8198479d330e 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -651,8 +651,7 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_co= ntext *ctx, shm =3D optee_rpc_cmd_alloc_suppl(ctx, sz); break; case OPTEE_RPC_SHM_TYPE_KERNEL: - shm =3D tee_shm_alloc(optee->ctx, sz, - TEE_SHM_MAPPED | TEE_SHM_PRIV); + shm =3D tee_shm_alloc_priv_buf(optee->ctx, sz); break; default: arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; @@ -777,8 +776,7 @@ static void optee_handle_rpc(struct tee_context *ctx, =20 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) { case OPTEE_SMC_RPC_FUNC_ALLOC: - shm =3D tee_shm_alloc(optee->ctx, param->a1, - TEE_SHM_MAPPED | TEE_SHM_PRIV); + shm =3D tee_shm_alloc_priv_buf(optee->ctx, param->a1); if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) { reg_pair_from_64(¶m->a1, ¶m->a2, pa); reg_pair_from_64(¶m->a4, ¶m->a5, diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index f0a9cccd2f2c..dd748d572691 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -49,25 +49,14 @@ static void tee_shm_release(struct tee_device *teedev, = struct tee_shm *shm) tee_device_put(teedev); } =20 -struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 fl= ags) +static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t si= ze, + size_t align, u32 flags, int id) { struct tee_device *teedev =3D ctx->teedev; struct tee_shm *shm; - size_t align; void *ret; int rc; =20 - if (!(flags & TEE_SHM_MAPPED)) { - dev_err(teedev->dev.parent, - "only mapped allocations supported\n"); - return ERR_PTR(-EINVAL); - } - - if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_PRIV))) { - dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags); - return ERR_PTR(-EINVAL); - } - if (!tee_device_get(teedev)) return ERR_PTR(-EINVAL); =20 @@ -84,18 +73,16 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, = size_t size, u32 flags) } =20 refcount_set(&shm->refcount, 1); - shm->flags =3D flags | TEE_SHM_POOL; + shm->flags =3D flags; + shm->id =3D id; + + /* + * We're assigning this as it is needed if the shm is to be + * registered. If this function returns OK then the caller expected + * to call teedev_ctx_get() or clear shm->ctx in case it's not + * needed any longer. + */ shm->ctx =3D ctx; - if (flags & TEE_SHM_DMA_BUF) { - align =3D PAGE_SIZE; - /* - * Request to register the shm in the pool allocator below - * if supported. - */ - shm->flags |=3D TEE_SHM_REGISTER; - } else { - align =3D 2 * sizeof(long); - } =20 rc =3D teedev->pool->ops->alloc(teedev->pool, shm, size, align); if (rc) { @@ -103,28 +90,14 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx,= size_t size, u32 flags) goto err_kfree; } =20 - if (flags & TEE_SHM_DMA_BUF) { - mutex_lock(&teedev->mutex); - shm->id =3D idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); - mutex_unlock(&teedev->mutex); - if (shm->id < 0) { - ret =3D ERR_PTR(shm->id); - goto err_pool_free; - } - } - teedev_ctx_get(ctx); - return shm; -err_pool_free: - teedev->pool->ops->free(teedev->pool, shm); err_kfree: kfree(shm); err_dev_put: tee_device_put(teedev); return ret; } -EXPORT_SYMBOL_GPL(tee_shm_alloc); =20 /** * tee_shm_alloc_user_buf() - Allocate shared memory for user space @@ -140,7 +113,36 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc); */ struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t siz= e) { - return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + u32 flags =3D TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_REGISTER | + TEE_SHM_POOL; + struct tee_device *teedev =3D ctx->teedev; + struct tee_shm *shm; + void *ret; + int id; + + mutex_lock(&teedev->mutex); + id =3D idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL); + mutex_unlock(&teedev->mutex); + if (id < 0) + return ERR_PTR(id); + + shm =3D shm_alloc_helper(ctx, size, PAGE_SIZE, flags, id); + if (IS_ERR(shm)) { + mutex_lock(&teedev->mutex); + idr_remove(&teedev->idr, id); + mutex_unlock(&teedev->mutex); + return shm; + } + + mutex_lock(&teedev->mutex); + ret =3D idr_replace(&teedev->idr, shm, id); + mutex_unlock(&teedev->mutex); + if (IS_ERR(ret)) { + tee_shm_free(shm); + return ret; + } + + return shm; } =20 /** @@ -157,10 +159,36 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_con= text *ctx, size_t size) */ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t s= ize) { - return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED); + u32 flags =3D TEE_SHM_MAPPED | TEE_SHM_REGISTER | TEE_SHM_POOL; + + return shm_alloc_helper(ctx, size, PAGE_SIZE, flags, -1); } EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); =20 +/** + * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared + * kernel buffer + * @ctx: Context that allocates the shared memory + * @size: Requested size of shared memory + * + * This function returns similar shared memory as + * tee_shm_alloc_kernel_buf(), but with the difference that the memory + * might not be registered in secure world in case the driver supports + * passing memory not registered in advance. + * + * This function should normally only be used internally in the TEE + * drivers. + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t siz= e) +{ + u32 flags =3D TEE_SHM_MAPPED | TEE_SHM_PRIV | TEE_SHM_POOL; + + return shm_alloc_helper(ctx, size, sizeof(long) * 2, flags, -1); +} +EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); + struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long ad= dr, size_t length, u32 flags) { diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index ed641dc314bd..7f038f8787c7 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -273,21 +273,7 @@ static inline void tee_shm_pool_free(struct tee_shm_po= ol *pool) */ void *tee_get_drvdata(struct tee_device *teedev); =20 -/** - * tee_shm_alloc() - Allocate shared memory - * @ctx: Context that allocates the shared memory - * @size: Requested size of shared memory - * @flags: Flags setting properties for the requested shared memory. - * - * Memory allocated as global shared memory is automatically freed when the - * TEE file pointer is closed. The @flags field uses the bits defined by - * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If - * TEE_SHM_DMA_BUF global shared memory will be allocated and associated - * with a dma-buf handle, else driver private memory. - * - * @returns a pointer to 'struct tee_shm' - */ -struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 fl= ags); +struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t siz= e); struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t s= ize); =20 /** --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 DC5A2C433F5 for ; Fri, 4 Feb 2022 09:34:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357689AbiBDJed (ORCPT ); Fri, 4 Feb 2022 04:34:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346715AbiBDJeS (ORCPT ); Fri, 4 Feb 2022 04:34:18 -0500 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15F5EC061744 for ; Fri, 4 Feb 2022 01:34:18 -0800 (PST) Received: by mail-lf1-x133.google.com with SMTP id a28so11430524lfl.7 for ; Fri, 04 Feb 2022 01:34:18 -0800 (PST) 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=l5cCpSDuv/t5xo91QXmJBGTql1lRnAMifhmqKFFJLsY=; b=N0OHNtGfvyYbJh6Nl6pPHVf3BMgGzzZiW6GzRJhBYom3F9uEsE3B8j3SqJMRJQLrkA h4Mjd7DvDAVyfOJgP1zlIJqPc2FpPwyVmlKnpbHZxBKH70qYxbEK0sWRtiQZduUxkvXr oIJki6xPBJO2uaqUb9uqyrWSy0ndXrEQASMHgfj9K1tPWsiXJi2XHaIBlHPp2MeTslKz 3XVdcIS1gM0XOs0MAp9b/JLOLYQEYz/GtenySUnJm4FUbkY31+bWCDvsbGkz/2XWhVmc ki8Vh12yPrZMhLW85Al7LkadCjsvwS5OlnBlw1sCaVHxiH4WJF4DFIXrWVwMoJ0Q6H+m M14g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=l5cCpSDuv/t5xo91QXmJBGTql1lRnAMifhmqKFFJLsY=; b=JO8qH0LOqtcZm9b0ySGjNaRX+j0FqmEiduiaxvGzGsnMZVmzoUPmJn0twNjmitjzft s1dywdlXv8U9fmerDXQlm8YC9t3nzX+BSrKumPqELbfQs4mFunT5M+NTeJGSUTa88bgH Hwj2/XoS8wpGJm3cwO9ldb0c0O9oUJWx+GVSeHMx8oK//8pnoURvspkIG6hnhVGRbM8o 3iGFRsZgaRu8DGgCrs1CWbnocojp9e7FY2EJq32pir01gyG9ctB536TcTUBs+6MxpAM8 TCTP7Sk6A9QrN4jBbzlO2Uv7xtVfcse79fsoIhfPloXmUJz/cl/jHG/GG9rzPmWSl8BA Wt6Q== X-Gm-Message-State: AOAM533JfEoksJ0fH8Zw9orDEyCV86gOc7ivoz8J9OhfghKaJEui3stV yCHKBwHZMZ1CJ1SA9S/HTPKM42TIO5F39w== X-Google-Smtp-Source: ABdhPJzyo3dc6L2SBwh8vz1vJy95dhwgN2ZHNs0JalsSw4rbaCCLXKpk/Fixc6x9ZQDSfjXdi6ZmZQ== X-Received: by 2002:a05:6512:10d6:: with SMTP id k22mr1766955lfg.243.1643967256168; Fri, 04 Feb 2022 01:34:16 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:15 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 06/10] optee: add optee_pool_op_free_helper() Date: Fri, 4 Feb 2022 10:33:55 +0100 Message-Id: <20220204093359.359059-7-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adds a common helper function to free a tee_shm allocated using the helper function optee_pool_op_alloc_helper(). Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/core.c | 10 ++++++++++ drivers/tee/optee/ffa_abi.c | 4 +--- drivers/tee/optee/optee_private.h | 3 +++ drivers/tee/optee/smc_abi.c | 7 +++---- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index f4bccb5f0e93..daf947e98d14 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -69,6 +69,16 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *pool= , struct tee_shm *shm, return rc; } =20 +void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *= shm, + int (*shm_unregister)(struct tee_context *ctx, + struct tee_shm *shm)) +{ + if (shm_unregister) + shm_unregister(shm->ctx, shm); + free_pages((unsigned long)shm->kaddr, get_order(shm->size)); + shm->kaddr =3D NULL; +} + static void optee_bus_scan(struct work_struct *work) { WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP)); diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index bf85975c6e95..695f73becf6a 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -379,9 +379,7 @@ static int pool_ffa_op_alloc(struct tee_shm_pool *pool, static void pool_ffa_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { - optee_ffa_shm_unregister(shm->ctx, shm); - free_pages((unsigned long)shm->kaddr, get_order(shm->size)); - shm->kaddr =3D NULL; + optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister); } =20 static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool) diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index df3a483bbf46..e77765c78878 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -236,6 +236,9 @@ int optee_pool_op_alloc_helper(struct tee_shm_pool *poo= l, struct tee_shm *shm, struct page **pages, size_t num_pages, unsigned long start)); +void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *= shm, + int (*shm_unregister)(struct tee_context *ctx, + struct tee_shm *shm)); =20 =20 void optee_remove_common(struct optee *optee); diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 8198479d330e..0b68127a8afa 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -548,10 +548,9 @@ static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { if (!(shm->flags & TEE_SHM_PRIV)) - optee_shm_unregister(shm->ctx, shm); - - free_pages((unsigned long)shm->kaddr, get_order(shm->size)); - shm->kaddr =3D NULL; + optee_pool_op_free_helper(pool, shm, optee_shm_unregister); + else + optee_pool_op_free_helper(pool, shm, NULL); } =20 static void pool_op_destroy_pool(struct tee_shm_pool *pool) --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 8595CC433F5 for ; Fri, 4 Feb 2022 09:34:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357772AbiBDJef (ORCPT ); Fri, 4 Feb 2022 04:34:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348314AbiBDJeT (ORCPT ); Fri, 4 Feb 2022 04:34:19 -0500 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3612CC06173D for ; Fri, 4 Feb 2022 01:34:19 -0800 (PST) Received: by mail-lf1-x134.google.com with SMTP id u14so11394814lfo.11 for ; Fri, 04 Feb 2022 01:34:19 -0800 (PST) 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=36Y39osWe7JWSD8/KyXS1kUOA5/n40+4mVVaerM2S7E=; b=zaLs+VFNkxv6Cdm2xEHZ9Aw5ajUqgSuHN2zAqjDi7hNA1y+dcn16jqP6ScynJb9XO2 LwP256THC5lB30rzbNcUYC9ho2hIkJby2D7n3hc26FVxDsYw9SuyK6TdIZLznuTC+rkQ juWFQ/GLquvKtJ0XwY9fLGqRBkrgjCb0jKpShFLVgEcz+EoZSdChAUqs8d9kBF+emNUN B2FjkHrxsuGzVlsD8EEShzRPqTTf8x8+IS9eZMzVaRpKL1EA3xqg1cwlF7dsy1B0a62A 4LzYkK2qdzQUUGXrB3rGBYXEwW6m9ODhwGHoC2jrHQw/U9qa5emvbZ4h6tglyGtjWtoV WQiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=36Y39osWe7JWSD8/KyXS1kUOA5/n40+4mVVaerM2S7E=; b=iLAQ/OjN5nxfPsD6IF8Pts0wQ4uRW2c6jgjz9JVs3lXptk+hcgJI8iAhiLTgeev0jD C+nwk7AWlvw0h5rsKh3ckmwseZT7B8+Yas+iiPxJJj215Q0qPgc9Zez4Nlg28okLlb+x XDHXc8WlpELtIh2sOjpg2hqkBI43GM+Y0WdnYvRHpPtkEIZq7Y7Reka1DmtNm0UcPvdE 3GblaUWyz7q4N/k6JFmvwKwyPNwdN9v1otLdK2fXW8p/Yt4lM0i9Br8uIWc0u6z9T5a5 H01xwl2/FcK7V7lOvTdj/JvltyQV/llWaBLkHMeeoV9NHxHkiec8T9Opu/fHUgCwJqQw kVug== X-Gm-Message-State: AOAM533vAtL+jjNPKMSJhiSTzuWfSaeatqmDwHHxiJ68ex0jX5cg7AwY xmPSfYB4GyRDX6oHvJmZj286AWDIKkDFFA== X-Google-Smtp-Source: ABdhPJyRMh0f+axqd/dZcvXOUop7vE5imy5AwTrFnL4fT4zqGChYfpSUPbGClesEtIdTK3eYS4+16A== X-Received: by 2002:ac2:454d:: with SMTP id j13mr1800189lfm.248.1643967257258; Fri, 04 Feb 2022 01:34:17 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:16 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 07/10] tee: add tee_shm_register_{user,kernel}_buf() Date: Fri, 4 Feb 2022 10:33:56 +0100 Message-Id: <20220204093359.359059-8-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adds the two new functions tee_shm_register_user_buf() and tee_shm_register_kernel_buf() which should be used instead of the old tee_shm_register(). This avoids having the caller supplying the flags parameter which exposes a bit more than desired of the internals of the TEE subsystem. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_core.c | 3 +-- drivers/tee/tee_private.h | 2 ++ drivers/tee/tee_shm.c | 33 +++++++++++++++++++++++++++++++++ include/linux/tee_drv.h | 2 ++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index a15812baaeb1..8aa1a4836b92 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -334,8 +334,7 @@ tee_ioctl_shm_register(struct tee_context *ctx, if (data.flags) return -EINVAL; =20 - shm =3D tee_shm_register(ctx, data.addr, data.length, - TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); + shm =3D tee_shm_register_user_buf(ctx, data.addr, data.length); if (IS_ERR(shm)) return PTR_ERR(shm); =20 diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 7265f47c6d8e..409cadcc1cff 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -58,5 +58,7 @@ void teedev_ctx_get(struct tee_context *ctx); void teedev_ctx_put(struct tee_context *ctx); =20 struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t siz= e); +struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, + unsigned long addr, size_t length); =20 #endif /*TEE_PRIVATE_H*/ diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index dd748d572691..359bab36e163 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -299,6 +299,39 @@ struct tee_shm *tee_shm_register(struct tee_context *c= tx, unsigned long addr, } EXPORT_SYMBOL_GPL(tee_shm_register); =20 +/** + * tee_shm_register_user_buf() - Register a userspace shared memory buffer + * @ctx: Context that registers the shared memory + * @addr: The userspace address of the shared buffer + * @length: Length of the shared buffer + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, + unsigned long addr, size_t length) +{ + return tee_shm_register(ctx, addr, length, + TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); +} + +/** + * tee_shm_register_kernel_buf() - Register kernel memory to be shared with + * secure world + * @ctx: Context that registers the shared memory + * @addr: The buffer + * @length: Length of the buffer + * + * @returns a pointer to 'struct tee_shm' + */ + +struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, + void *addr, size_t length) +{ + return tee_shm_register(ctx, (unsigned long)addr, length, + TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED); +} +EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf); + static int tee_shm_fop_release(struct inode *inode, struct file *filp) { tee_shm_put(filp->private_data); diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 7f038f8787c7..c9d2cc32a5ed 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -287,6 +287,8 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_con= text *ctx, size_t size); */ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long ad= dr, size_t length, u32 flags); +struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, + void *addr, size_t length); =20 /** * tee_shm_is_registered() - Check if shared memory object in registered i= n TEE --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 59E00C433F5 for ; Fri, 4 Feb 2022 09:34:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357846AbiBDJei (ORCPT ); Fri, 4 Feb 2022 04:34:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351159AbiBDJeU (ORCPT ); Fri, 4 Feb 2022 04:34:20 -0500 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DBE6C06173E for ; Fri, 4 Feb 2022 01:34:20 -0800 (PST) Received: by mail-lf1-x136.google.com with SMTP id u6so11415983lfm.10 for ; Fri, 04 Feb 2022 01:34:20 -0800 (PST) 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=IS0JYukQ1AvM/V854xXkjA8DA3zzQrPupG56zCEbY2A=; b=BV11LaWbT+uZvF/l/ofhsdy/6VN42Av/53aXa1sONswjmdtEl5IOQBghFsXAMWOIt4 rbFwq4D/zncNVGL2GWOXdPhJkaZwCCHzkKR2/F3W1j6Zngbxo9zIbeecpD6CPvh1kJ87 lEkovKZ1w3u2IppcrJPM5l4UAC75+Ph702TC1JJE2OiYxqF5xNB46maMI6DGpgPIjGsa QnRQElPxsyhhgOavaW5k9RmWwTB2y2f93ksWIq9LkMs4JsUTuzMluTqlxxFIug4bg9xe NpMcbnZU2sPpDWSNrl0pteDJ7PkSZeTFn5SwgvLgyboKStiD2Hq7W7+XKo8uZsOIriMD 4GMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IS0JYukQ1AvM/V854xXkjA8DA3zzQrPupG56zCEbY2A=; b=l+veqFXlGqzbfh8MWgWXZvhF6pM42eSYaRUXd50e4s+YUSlk45gXTbQGvXH8/mqahR PEJI85g5bWlwYadnaxF0CYf3Hlic3DTU0WTzPYqIfxLr+9F2DqfslHOwZr5u6G9fnKtC X9L3CY6vKGYUMIzzqItSInBE7XuQeL5nMn+TLwaYQnWReqdD4tmtN5W6pbI7yuj3MOha 2/OeG3B1uPD6hvsbX4ybeBoSND4+qFU2BVsufqfQKwvp/cclqzJHJkvgpN1c9BBURsr+ h0wOC95esi7BPscRyL/gBicrNJT3bRA+kLWXwnYrN9TwxUUycOR4VWaNOPGXIkjxZCYu yTUQ== X-Gm-Message-State: AOAM531XgCS5mp1wLNyrWSv8w/v0VfJwqjO9NesUPm/EKmmHrY9DFPsP k3A3bApm7zvU9EsTLMaZkDq6qfL0kkgeYw== X-Google-Smtp-Source: ABdhPJyas4FuzuURUzUq8NzENbhxnd5Lfe/CyP+PE5eFl4zIzdMOtpyCqeFsb6RpJSEnBfZaBExpjg== X-Received: by 2002:a19:dc4b:: with SMTP id f11mr328419lfj.152.1643967258337; Fri, 04 Feb 2022 01:34:18 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:17 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 08/10] KEYS: trusted: tee: use tee_shm_register_kernel_buf() Date: Fri, 4 Feb 2022 10:33:57 +0100 Message-Id: <20220204093359.359059-9-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Uses the new simplified tee_shm_register_kernel_buf() function instead of the old tee_shm_alloc() function which required specific TEE_SHM-flags Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- security/keys/trusted-keys/trusted_tee.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trust= ed-keys/trusted_tee.c index 2ce66c199e1d..c8626686ee1b 100644 --- a/security/keys/trusted-keys/trusted_tee.c +++ b/security/keys/trusted-keys/trusted_tee.c @@ -70,17 +70,15 @@ static int trusted_tee_seal(struct trusted_key_payload = *p, char *datablob) memset(&inv_arg, 0, sizeof(inv_arg)); memset(¶m, 0, sizeof(param)); =20 - reg_shm_in =3D tee_shm_register(pvt_data.ctx, (unsigned long)p->key, - p->key_len, TEE_SHM_DMA_BUF | - TEE_SHM_KERNEL_MAPPED); + reg_shm_in =3D tee_shm_register_kernel_buf(pvt_data.ctx, p->key, + p->key_len); if (IS_ERR(reg_shm_in)) { dev_err(pvt_data.dev, "key shm register failed\n"); return PTR_ERR(reg_shm_in); } =20 - reg_shm_out =3D tee_shm_register(pvt_data.ctx, (unsigned long)p->blob, - sizeof(p->blob), TEE_SHM_DMA_BUF | - TEE_SHM_KERNEL_MAPPED); + reg_shm_out =3D tee_shm_register_kernel_buf(pvt_data.ctx, p->blob, + sizeof(p->blob)); if (IS_ERR(reg_shm_out)) { dev_err(pvt_data.dev, "blob shm register failed\n"); ret =3D PTR_ERR(reg_shm_out); @@ -131,17 +129,15 @@ static int trusted_tee_unseal(struct trusted_key_payl= oad *p, char *datablob) memset(&inv_arg, 0, sizeof(inv_arg)); memset(¶m, 0, sizeof(param)); =20 - reg_shm_in =3D tee_shm_register(pvt_data.ctx, (unsigned long)p->blob, - p->blob_len, TEE_SHM_DMA_BUF | - TEE_SHM_KERNEL_MAPPED); + reg_shm_in =3D tee_shm_register_kernel_buf(pvt_data.ctx, p->blob, + p->blob_len); if (IS_ERR(reg_shm_in)) { dev_err(pvt_data.dev, "blob shm register failed\n"); return PTR_ERR(reg_shm_in); } =20 - reg_shm_out =3D tee_shm_register(pvt_data.ctx, (unsigned long)p->key, - sizeof(p->key), TEE_SHM_DMA_BUF | - TEE_SHM_KERNEL_MAPPED); + reg_shm_out =3D tee_shm_register_kernel_buf(pvt_data.ctx, p->key, + sizeof(p->key)); if (IS_ERR(reg_shm_out)) { dev_err(pvt_data.dev, "key shm register failed\n"); ret =3D PTR_ERR(reg_shm_out); @@ -192,8 +188,7 @@ static int trusted_tee_get_random(unsigned char *key, s= ize_t key_len) memset(&inv_arg, 0, sizeof(inv_arg)); memset(¶m, 0, sizeof(param)); =20 - reg_shm =3D tee_shm_register(pvt_data.ctx, (unsigned long)key, key_len, - TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED); + reg_shm =3D tee_shm_register_kernel_buf(pvt_data.ctx, key, key_len); if (IS_ERR(reg_shm)) { dev_err(pvt_data.dev, "key shm register failed\n"); return PTR_ERR(reg_shm); --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 A1DA2C433EF for ; Fri, 4 Feb 2022 09:34:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357960AbiBDJeo (ORCPT ); Fri, 4 Feb 2022 04:34:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357273AbiBDJeW (ORCPT ); Fri, 4 Feb 2022 04:34:22 -0500 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61AFEC061744 for ; Fri, 4 Feb 2022 01:34:21 -0800 (PST) Received: by mail-lf1-x12e.google.com with SMTP id z19so11392446lfq.13 for ; Fri, 04 Feb 2022 01:34:21 -0800 (PST) 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=zw4+qaaeyE/aWXMXtlVwIinAenGGPkkzb74yx3Vt6tc=; b=i6GPnvu2iaZlXdEVJFZqG0Y384fIX2eykdqvYElw2MNIj1ud9S/XRwlTZyeihI0cDn +V4SOaPIHbjEiitkdPvGqq5PkM8h+brsDurk4oi2bohpRwnb54zvyK8ZVaicnmy3+vgZ AOPxx8M7/XhKLCtkHUZ9ZnwC1y3yyvXr1e22leM00GzFM5Fpa2L8vIfxKdVMccku8KLp uyCprk07RM89yhuI1NWwWlji09Vro/ZYj/vHL2olCooad0AepVzJ9Vy7mjdSjKmoP0T4 LWRPO6Adh7kvIEhinserVpfh/tH8ztAIKISSTWtvLj6hl0OmZxacywNMlmBMDgKLHzUf lOmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zw4+qaaeyE/aWXMXtlVwIinAenGGPkkzb74yx3Vt6tc=; b=kveF/4Y/umxrG3sD/3WLtfgdSZOXoGP/DxTCJzdKgDul9iZov1go/uS2/2+KpCp+A2 YrM4fppFqJEK4X7BVUIkHEbdeO9OOYAXoBJqTrHsLU7AT7P2/NusH9p729ucx2llxF2q abWpegRDbcJhOa4Y6Fe3Y8bbQIzMhb2OAojpVUO/qq/6mf0WRQzTba3VhfYZt6/bfnSL +amzlSZAGvu3ijdrNR8UN7iToiktUcHhbBuJ/jirjd8uVjR3kmNF+u8zNz2vnYmqlvrB hVJmMg5GERRerB1+4d/+plIVjkNw0pko9GqrLwN6tuSfKStpTSG5lefNI6/JVGlR/rV9 AKwg== X-Gm-Message-State: AOAM5314R1M+MCzctlo8bUFK1Kf0rMDzm97NfduxyakdduLhtlSqjEEV cdCl+75AcKyAF4R46Q4R/u53FRcAi+uMmQ== X-Google-Smtp-Source: ABdhPJxdKNvOzkwoCUaInqlGAuq8UlfyL1XDiisFZh0juQ76heOgAcvtm/6OqQDrEjDz+U32dCFAew== X-Received: by 2002:a05:6512:ea1:: with SMTP id bi33mr1782955lfb.571.1643967259396; Fri, 04 Feb 2022 01:34:19 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:18 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 09/10] tee: replace tee_shm_register() Date: Fri, 4 Feb 2022 10:33:58 +0100 Message-Id: <20220204093359.359059-10-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" tee_shm_register() is replaced by the previously introduced functions tee_shm_register_user_buf() and tee_shm_register_kernel_buf(). Since there are not external callers left we can remove tee_shm_register() and refactor the remains. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_shm.c | 156 +++++++++++++++++++++++----------------- include/linux/tee_drv.h | 11 --- 2 files changed, 90 insertions(+), 77 deletions(-) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 359bab36e163..9db571253802 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -12,17 +12,43 @@ #include #include "tee_private.h" =20 +static void shm_put_kernel_pages(struct page **pages, size_t page_count) +{ + size_t n; + + for (n =3D 0; n < page_count; n++) + put_page(pages[n]); +} + +static int shm_get_kernel_pages(unsigned long start, size_t page_count, + struct page **pages) +{ + struct kvec *kiov; + size_t n; + int rc; + + kiov =3D kcalloc(page_count, sizeof(*kiov), GFP_KERNEL); + if (!kiov) + return -ENOMEM; + + for (n =3D 0; n < page_count; n++) { + kiov[n].iov_base =3D (void *)(start + n * PAGE_SIZE); + kiov[n].iov_len =3D PAGE_SIZE; + } + + rc =3D get_kernel_pages(kiov, page_count, 0, pages); + kfree(kiov); + + return rc; +} + static void release_registered_pages(struct tee_shm *shm) { if (shm->pages) { - if (shm->flags & TEE_SHM_USER_MAPPED) { + if (shm->flags & TEE_SHM_USER_MAPPED) unpin_user_pages(shm->pages, shm->num_pages); - } else { - size_t n; - - for (n =3D 0; n < shm->num_pages; n++) - put_page(shm->pages[n]); - } + else + shm_put_kernel_pages(shm->pages, shm->num_pages); =20 kfree(shm->pages); } @@ -189,28 +215,24 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_con= text *ctx, size_t size) } EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); =20 -struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long ad= dr, - size_t length, u32 flags) +static struct tee_shm * +register_shm_helper(struct tee_context *ctx, unsigned long addr, + size_t length, u32 flags, int id) { struct tee_device *teedev =3D ctx->teedev; - const u32 req_user_flags =3D TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED; - const u32 req_kernel_flags =3D TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED; struct tee_shm *shm; + unsigned long start; + size_t num_pages; void *ret; int rc; - int num_pages; - unsigned long start; - - if (flags !=3D req_user_flags && flags !=3D req_kernel_flags) - return ERR_PTR(-ENOTSUPP); =20 if (!tee_device_get(teedev)) return ERR_PTR(-EINVAL); =20 if (!teedev->desc->ops->shm_register || !teedev->desc->ops->shm_unregister) { - tee_device_put(teedev); - return ERR_PTR(-ENOTSUPP); + ret =3D ERR_PTR(-ENOTSUPP); + goto err_dev_put; } =20 teedev_ctx_get(ctx); @@ -218,13 +240,13 @@ struct tee_shm *tee_shm_register(struct tee_context *= ctx, unsigned long addr, shm =3D kzalloc(sizeof(*shm), GFP_KERNEL); if (!shm) { ret =3D ERR_PTR(-ENOMEM); - goto err; + goto err_ctx_put; } =20 refcount_set(&shm->refcount, 1); - shm->flags =3D flags | TEE_SHM_REGISTER; + shm->flags =3D flags; shm->ctx =3D ctx; - shm->id =3D -1; + shm->id =3D id; addr =3D untagged_addr(addr); start =3D rounddown(addr, PAGE_SIZE); shm->offset =3D addr - start; @@ -233,71 +255,45 @@ struct tee_shm *tee_shm_register(struct tee_context *= ctx, unsigned long addr, shm->pages =3D kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL); if (!shm->pages) { ret =3D ERR_PTR(-ENOMEM); - goto err; + goto err_free_shm; } =20 - if (flags & TEE_SHM_USER_MAPPED) { + if (flags & TEE_SHM_USER_MAPPED) rc =3D pin_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages); - } else { - struct kvec *kiov; - int i; - - kiov =3D kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL); - if (!kiov) { - ret =3D ERR_PTR(-ENOMEM); - goto err; - } - - for (i =3D 0; i < num_pages; i++) { - kiov[i].iov_base =3D (void *)(start + i * PAGE_SIZE); - kiov[i].iov_len =3D PAGE_SIZE; - } - - rc =3D get_kernel_pages(kiov, num_pages, 0, shm->pages); - kfree(kiov); - } + else + rc =3D shm_get_kernel_pages(start, num_pages, shm->pages); if (rc > 0) shm->num_pages =3D rc; if (rc !=3D num_pages) { if (rc >=3D 0) rc =3D -ENOMEM; ret =3D ERR_PTR(rc); - goto err; - } - - mutex_lock(&teedev->mutex); - shm->id =3D idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); - mutex_unlock(&teedev->mutex); - - if (shm->id < 0) { - ret =3D ERR_PTR(shm->id); - goto err; + goto err_put_shm_pages; } =20 rc =3D teedev->desc->ops->shm_register(ctx, shm, shm->pages, shm->num_pages, start); if (rc) { ret =3D ERR_PTR(rc); - goto err; + goto err_put_shm_pages; } =20 return shm; -err: - if (shm) { - if (shm->id >=3D 0) { - mutex_lock(&teedev->mutex); - idr_remove(&teedev->idr, shm->id); - mutex_unlock(&teedev->mutex); - } - release_registered_pages(shm); - } +err_put_shm_pages: + if (flags & TEE_SHM_USER_MAPPED) + unpin_user_pages(shm->pages, shm->num_pages); + else + shm_put_kernel_pages(shm->pages, shm->num_pages); + kfree(shm->pages); +err_free_shm: kfree(shm); +err_ctx_put: teedev_ctx_put(ctx); +err_dev_put: tee_device_put(teedev); return ret; } -EXPORT_SYMBOL_GPL(tee_shm_register); =20 /** * tee_shm_register_user_buf() - Register a userspace shared memory buffer @@ -310,8 +306,35 @@ EXPORT_SYMBOL_GPL(tee_shm_register); struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, unsigned long addr, size_t length) { - return tee_shm_register(ctx, addr, length, - TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); + u32 flags =3D TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED | TEE_SHM_REGISTER; + struct tee_device *teedev =3D ctx->teedev; + struct tee_shm *shm; + void *ret; + int id; + + mutex_lock(&teedev->mutex); + id =3D idr_alloc(&teedev->idr, NULL, 1, 0, GFP_KERNEL); + mutex_unlock(&teedev->mutex); + if (id < 0) + return ERR_PTR(id); + + shm =3D register_shm_helper(ctx, addr, length, flags, id); + if (IS_ERR(shm)) { + mutex_lock(&teedev->mutex); + idr_remove(&teedev->idr, id); + mutex_unlock(&teedev->mutex); + return shm; + } + + mutex_lock(&teedev->mutex); + ret =3D idr_replace(&teedev->idr, shm, id); + mutex_unlock(&teedev->mutex); + if (IS_ERR(ret)) { + tee_shm_free(shm); + return ret; + } + + return shm; } =20 /** @@ -327,8 +350,9 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_co= ntext *ctx, struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length) { - return tee_shm_register(ctx, (unsigned long)addr, length, - TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED); + u32 flags =3D TEE_SHM_REGISTER | TEE_SHM_KERNEL_MAPPED; + + return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1); } EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf); =20 diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index c9d2cc32a5ed..a3b663ef0694 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -276,17 +276,6 @@ void *tee_get_drvdata(struct tee_device *teedev); struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t siz= e); struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t s= ize); =20 -/** - * tee_shm_register() - Register shared memory buffer - * @ctx: Context that registers the shared memory - * @addr: Address is userspace of the shared buffer - * @length: Length of the shared buffer - * @flags: Flags setting properties for the requested shared memory. - * - * @returns a pointer to 'struct tee_shm' - */ -struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long ad= dr, - size_t length, u32 flags); struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length); =20 --=20 2.31.1 From nobody Mon Jun 29 18:39:31 2026 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 8DB1BC433F5 for ; Fri, 4 Feb 2022 09:34:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358039AbiBDJet (ORCPT ); Fri, 4 Feb 2022 04:34:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347693AbiBDJeW (ORCPT ); Fri, 4 Feb 2022 04:34:22 -0500 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1BAAC061749 for ; Fri, 4 Feb 2022 01:34:22 -0800 (PST) Received: by mail-lf1-x12f.google.com with SMTP id bu18so11484509lfb.5 for ; Fri, 04 Feb 2022 01:34:22 -0800 (PST) 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=5yxwOzLChV0nlm4fspnb3kRsF537bDz0hCyoL1WhejM=; b=LLeppM51aXShBamlgRBIU6V6i4xBhv8p9AW/Axk2xpKaEoBZYXg97YkqTx0juvDiGD gP7j0iJ6h16gGVuLXhYQPfC1fTPObxfYliebQUG+uxqqsJ+rhCMGRMBK29o5wOhZZqxS sZoO0XJ3p40X/9anCo564C90es/nwYO32c9RfvPE3nCvqI+4h3bF9sgITN06Nb7X4Z0+ XMoTNd9Ld085luzbMYtkGD2UAUeun9DVcxHgMTSZV36pGxaQiIwhWRduhFu7Sxe92YCe nm8BkGI7WlRYLwL5Efh35x2b74Ie0K9Tmprq+OE5GLOAZXiQQCTkdVGjm55picphqQCB tvMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5yxwOzLChV0nlm4fspnb3kRsF537bDz0hCyoL1WhejM=; b=FpimL5xaun+MOVLhFMO01s0C3tuuYHTfWtN4Twu3FA7Oett1Rt7BdCE50vcFL8hlYm mBGWMQ11MAPy3GD3Y1r58KyjAhNWQE6GQNtChNOj2//LVny1yBT3ekkytbNEFv7V7DZW +xUxhAKhSO4B35L5nYKugyxXdeNkYlKg8OEZkk5x7XRtvqO8tA19Xr+g6lJVNKiJzwKf JnCmjanCyZHW60I5OvqoorZvvfHy6/Cc7HYkgONlbBrwOVSoreTcYe68OQU1+/8gzANe nViwaeYt2yd/gcUxIJf8E+Fi4QbjfrJHGmtVSi9ovYhvwtKuFFoRsKT1VJ7w8KuCwfJx X3Jw== X-Gm-Message-State: AOAM530kgS0IvabrN54fXqS7lUm4dCxgcMeJzg+JXk6u34hj1UnERzAx OQNE3Yum+MQ22MRI7U9YrZnZpNp0evC9xw== X-Google-Smtp-Source: ABdhPJxbYt/ERKyQoTGpi/Y14rpMGaMhONsrRNgYsiPd5ZAK9vMo1582tbugBYGfIWGrD9nU0ZTirQ== X-Received: by 2002:a05:6512:68e:: with SMTP id t14mr1784843lfe.366.1643967260703; Fri, 04 Feb 2022 01:34:20 -0800 (PST) Received: from jade.urgonet (h-94-254-48-165.A175.priv.bahnhof.se. [94.254.48.165]) by smtp.gmail.com with ESMTPSA id u28sm230550lfl.160.2022.02.04.01.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 01:34:20 -0800 (PST) From: Jens Wiklander To: linux-kernel@vger.kernel.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Devaraj Rangasamy , Rijo Thomas , David Howells , Tyler Hicks , Jens Wiklander Subject: [PATCH v4 10/10] tee: refactor TEE_SHM_* flags Date: Fri, 4 Feb 2022 10:33:59 +0100 Message-Id: <20220204093359.359059-11-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204093359.359059-1-jens.wiklander@linaro.org> References: <20220204093359.359059-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Removes the redundant TEE_SHM_DMA_BUF, TEE_SHM_EXT_DMA_BUF, TEE_SHM_MAPPED and TEE_SHM_KERNEL_MAPPED flags. TEE_SHM_REGISTER is renamed to TEE_SHM_DYNAMIC in order to better match its usage. Assigns new values to the remaining flags to void gaps. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/smc_abi.c | 4 ++-- drivers/tee/tee_shm.c | 23 +++++++++++------------ drivers/tee/tee_shm_pool.c | 2 +- include/linux/tee_drv.h | 21 +++++++++------------ 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 0b68127a8afa..375a40eef36c 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -228,7 +228,7 @@ static int optee_to_msg_param(struct optee *optee, case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: - if (tee_shm_is_registered(p->u.memref.shm)) + if (tee_shm_is_dynamic(p->u.memref.shm)) rc =3D to_msg_param_reg_mem(mp, p); else rc =3D to_msg_param_tmp_mem(mp, p); @@ -669,7 +669,7 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_co= ntext *ctx, =20 sz =3D tee_shm_get_size(shm); =20 - if (tee_shm_is_registered(shm)) { + if (tee_shm_is_dynamic(shm)) { struct page **pages; u64 *pages_list; size_t page_num; diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 9db571253802..f31e29e8f1ca 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -58,7 +58,7 @@ static void tee_shm_release(struct tee_device *teedev, st= ruct tee_shm *shm) { if (shm->flags & TEE_SHM_POOL) { teedev->pool->ops->free(teedev->pool, shm); - } else if (shm->flags & TEE_SHM_REGISTER) { + } else if (shm->flags & TEE_SHM_DYNAMIC) { int rc =3D teedev->desc->ops->shm_unregister(shm->ctx, shm); =20 if (rc) @@ -139,8 +139,7 @@ static struct tee_shm *shm_alloc_helper(struct tee_cont= ext *ctx, size_t size, */ struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t siz= e) { - u32 flags =3D TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_REGISTER | - TEE_SHM_POOL; + u32 flags =3D TEE_SHM_DYNAMIC | TEE_SHM_POOL; struct tee_device *teedev =3D ctx->teedev; struct tee_shm *shm; void *ret; @@ -185,7 +184,7 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_conte= xt *ctx, size_t size) */ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t s= ize) { - u32 flags =3D TEE_SHM_MAPPED | TEE_SHM_REGISTER | TEE_SHM_POOL; + u32 flags =3D TEE_SHM_DYNAMIC | TEE_SHM_POOL; =20 return shm_alloc_helper(ctx, size, PAGE_SIZE, flags, -1); } @@ -209,7 +208,7 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); */ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t siz= e) { - u32 flags =3D TEE_SHM_MAPPED | TEE_SHM_PRIV | TEE_SHM_POOL; + u32 flags =3D TEE_SHM_PRIV | TEE_SHM_POOL; =20 return shm_alloc_helper(ctx, size, sizeof(long) * 2, flags, -1); } @@ -306,7 +305,7 @@ register_shm_helper(struct tee_context *ctx, unsigned l= ong addr, struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, unsigned long addr, size_t length) { - u32 flags =3D TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED | TEE_SHM_REGISTER; + u32 flags =3D TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC; struct tee_device *teedev =3D ctx->teedev; struct tee_shm *shm; void *ret; @@ -350,7 +349,7 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_co= ntext *ctx, struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length) { - u32 flags =3D TEE_SHM_REGISTER | TEE_SHM_KERNEL_MAPPED; + u32 flags =3D TEE_SHM_DYNAMIC; =20 return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1); } @@ -394,7 +393,7 @@ int tee_shm_get_fd(struct tee_shm *shm) { int fd; =20 - if (!(shm->flags & TEE_SHM_DMA_BUF)) + if (shm->id < 0) return -EINVAL; =20 /* matched by tee_shm_put() in tee_shm_op_release() */ @@ -424,7 +423,7 @@ EXPORT_SYMBOL_GPL(tee_shm_free); */ int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa) { - if (!(shm->flags & TEE_SHM_MAPPED)) + if (!shm->kaddr) return -EINVAL; /* Check that we're in the range of the shm */ if ((char *)va < (char *)shm->kaddr) @@ -446,7 +445,7 @@ EXPORT_SYMBOL_GPL(tee_shm_va2pa); */ int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va) { - if (!(shm->flags & TEE_SHM_MAPPED)) + if (!shm->kaddr) return -EINVAL; /* Check that we're in the range of the shm */ if (pa < shm->paddr) @@ -474,7 +473,7 @@ EXPORT_SYMBOL_GPL(tee_shm_pa2va); */ void *tee_shm_get_va(struct tee_shm *shm, size_t offs) { - if (!(shm->flags & TEE_SHM_MAPPED)) + if (!shm->kaddr) return ERR_PTR(-EINVAL); if (offs >=3D shm->size) return ERR_PTR(-EINVAL); @@ -549,7 +548,7 @@ void tee_shm_put(struct tee_shm *shm) * the refcount_inc() in tee_shm_get_from_id() never starts * from 0. */ - if (shm->flags & TEE_SHM_DMA_BUF) + if (shm->id >=3D 0) idr_remove(&teedev->idr, shm->id); do_release =3D true; } diff --git a/drivers/tee/tee_shm_pool.c b/drivers/tee/tee_shm_pool.c index 71e0f8ae69aa..058bfbac657a 100644 --- a/drivers/tee/tee_shm_pool.c +++ b/drivers/tee/tee_shm_pool.c @@ -30,7 +30,7 @@ static int pool_op_gen_alloc(struct tee_shm_pool *pool, s= truct tee_shm *shm, * This is from a static shared memory pool so no need to register * each chunk, and no need to unregister later either. */ - shm->flags &=3D ~TEE_SHM_REGISTER; + shm->flags &=3D ~TEE_SHM_DYNAMIC; return 0; } =20 diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index a3b663ef0694..911cad324acc 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -20,14 +20,11 @@ * specific TEE driver. */ =20 -#define TEE_SHM_MAPPED BIT(0) /* Memory mapped by the kernel */ -#define TEE_SHM_DMA_BUF BIT(1) /* Memory with dma-buf handle */ -#define TEE_SHM_EXT_DMA_BUF BIT(2) /* Memory with dma-buf handle */ -#define TEE_SHM_REGISTER BIT(3) /* Memory registered in secure world */ -#define TEE_SHM_USER_MAPPED BIT(4) /* Memory mapped in user space */ -#define TEE_SHM_POOL BIT(5) /* Memory allocated from pool */ -#define TEE_SHM_KERNEL_MAPPED BIT(6) /* Memory mapped in kernel space */ -#define TEE_SHM_PRIV BIT(7) /* Memory private to TEE driver */ +#define TEE_SHM_DYNAMIC BIT(0) /* Dynamic shared memory registered */ + /* in secure world */ +#define TEE_SHM_USER_MAPPED BIT(1) /* Memory mapped in user space */ +#define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */ +#define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */ =20 struct device; struct tee_device; @@ -280,13 +277,13 @@ struct tee_shm *tee_shm_register_kernel_buf(struct te= e_context *ctx, void *addr, size_t length); =20 /** - * tee_shm_is_registered() - Check if shared memory object in registered i= n TEE + * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic = kind * @shm: Shared memory handle - * @returns true if object is registered in TEE + * @returns true if object is dynamic shared memory */ -static inline bool tee_shm_is_registered(struct tee_shm *shm) +static inline bool tee_shm_is_dynamic(struct tee_shm *shm) { - return shm && (shm->flags & TEE_SHM_REGISTER); + return shm && (shm->flags & TEE_SHM_DYNAMIC); } =20 /** --=20 2.31.1