From patchwork Wed Jun 9 10:23:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456814 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115900jae; Wed, 9 Jun 2021 03:23:38 -0700 (PDT) X-Received: by 2002:a2e:2f07:: with SMTP id v7mr21781350ljv.308.1623234218697; Wed, 09 Jun 2021 03:23:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234218; cv=none; d=google.com; s=arc-20160816; b=wkYSlHXK3tnWuisGAU9zVH0WBZ76kzaI70R22kmgYJwVRTv+t3P0dGqNkKDGCRdddI 4WRaR8tdZYxwCDm+t+giiYLEkjNmYS7Ek218PU/317oj7QDt+XcSFQRhFPCFgtogEkpJ 5jQ0IXRZqrS2EDg4Qc5rStf9y1D62C6GjGBdBVGqHzheG+wFqTP9ehlEk2/7S5okapFW eBlytSb14hbid+IVySpSpD2bpdlaI7p1p3+Tp8GtBaxaoc/Me4IXEXmOesg13BITldXa N5axmtgGW4WFiGpcZa7LKjmbQEO/Bxlnmz0T5MAG3XNdLpXzhAsuQAda4YBr/6DQhIOX TarA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=S9L1UST91CNAHLkSPICX3VPuq32tNYAEanaTNNZO0q8=; b=b/0U6ezHQUTGQoFz5favns2beHpVWswT8iHviyRVeh1oW4P3weArqtqut3J2viP0tk Lia4aP3mLfpkruhYltNBCY5rf/u6ENPjENZdVZmM8crl/mYRr4ESMq3psfDcZ3HIhn38 0gamonViULNirMt8kJRIEkuTcJCFVVKxI1BRZwOC/BIqRbUCcxIzlerYXoFFLRBrNSl2 KQtQ/DQy2/J6tWSscmpWQAzs9kQGsUD6xTsGo/rMvvmnkbwIt8G2XnoId4qMXh4wMBLi c6bbgQkyfiVbvgdIrsXp0iBDnjtVscbuRgwheruCW2kNlr76SLO1X6Yqgx+JOHyMzOxz w6BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IYOk8I54; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id x8sor630071lfe.44.2021.06.09.03.23.38 for (Google Transport Security); Wed, 09 Jun 2021 03:23:38 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IYOk8I54; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S9L1UST91CNAHLkSPICX3VPuq32tNYAEanaTNNZO0q8=; b=IYOk8I54rWLZ5Np2R1bzEFwVy5KwKmcdQ4BTvvnwJbTEsg6OiNsdY2CBZoYILdQfXx VLbfJxVpa66mnOx2V4bYHwexFzVqSD2UJGHdrWCJq9QbmwwI4d5aCOBk4JF2Ad11zEjm IjMcAVXyZVai5sdwgeKdfmWyTmzP6iwafOBPLyjsPlcZ30LrDMsZKJMy26zYNBUntUbO rfk8FwfoyHkiV4TDwAsiwv4ij4C0pJ1JZ3PKTCJjzxLQyPiLBeSOdK3bC2DlCDN4U2/f ySkUMzcFYkYlffC0o+Mp1osFIWtaYtSfD21GYwgC4VB0Hy6kKq1Y+vqsVZrNnWO/zzzs XuWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S9L1UST91CNAHLkSPICX3VPuq32tNYAEanaTNNZO0q8=; b=Y8aaceFv3FoqMeFPVMnUzxiyEk2EwGR8edoKqoRhCiFkiBHzb43w/1yls5yzbK57PR L2lViWCK299063BKgo++66ynuu6A+nm5WDzyBEmLca4kZiqKl9h+oJF1mti9f3o1A4eg SdM2moMr6/IKvdPiMV5CN5O3IMbrcJ7nrJ10T9gBd20yaMLKlX64QVHn1lTuzJTehOG2 i58vV+15wdUe5mka0TrkCNA7FRcVDx2nx9x4EPctQdMYAjQjLheR7nDpewsl09Q9OPKT 9b/xJsObT1qSzgw1LyS43T4zS7lfQ569d1R4xWb/JJnHwwRyVNL0/UlZ42RmhruwfO2f jN4A== X-Gm-Message-State: AOAM530cltplp1b8BqGITlees6fgg0HID5QT5/eshbjbLPxHEpAGEFGt 2qI9aWPXGESa/B60EVbFu8HoeGoJ X-Google-Smtp-Source: ABdhPJyq82dDCmEPp1H3l89ON8o2eLljF8JtSjMCVEN2Y9jxHB/b7oKBppGWd3jdmwYwhqXem/TJsg== X-Received: by 2002:a05:6512:3146:: with SMTP id s6mr18254687lfi.173.1623234218296; Wed, 09 Jun 2021 03:23:38 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:38 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 1/7] tee: remove unused tee_shm_pool_alloc_res_mem() Date: Wed, 9 Jun 2021 12:23:18 +0200 Message-Id: <20210609102324.2222332-2-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 None of the drivers in the TEE subsystem uses tee_shm_pool_alloc_res_mem() so remove the function. Signed-off-by: Jens Wiklander --- drivers/tee/tee_shm_pool.c | 56 -------------------------------------- include/linux/tee_drv.h | 30 -------------------- 2 files changed, 86 deletions(-) -- 2.31.1 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_generic = { .destroy_poolmgr = pool_op_gen_destroy_poolmgr, }; -/** - * 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 = 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 = rc; - - /* - * Create the pool for dma_buf shared memory - */ - rc = 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 = rc; - - rc = 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 vaddr, phys_addr_t paddr, size_t size, diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 54269e47ac9a..53b9b2a13a87 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -272,36 +272,6 @@ static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm) poolm->ops->destroy_poolmgr(poolm); } -/** - * struct tee_shm_pool_mem_info - holds information needed to create a shared - * 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 From patchwork Wed Jun 9 10:23:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456815 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115913jae; Wed, 9 Jun 2021 03:23:39 -0700 (PDT) X-Received: by 2002:ac2:5c4e:: with SMTP id s14mr18720418lfp.94.1623234219741; Wed, 09 Jun 2021 03:23:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234219; cv=none; d=google.com; s=arc-20160816; b=xaqhCP8t58PD/P+PNXU9F6ryB3xFzSFIm0b8+4LtwVQ7tWfRagHyAuFfEN2wbBSZ1I ic+V94PBz8LuJgQNs/M+wnjs5u1ObB1xL/VghZiiJKn1+cqDZFqr9x+z7zDaIN0cxenl omQ9xJWEjKTSkXMH/cbKN0gW8Xbx0dV3BPht7f2DQe5UuImn1wZPcYGNmbQ62BHMTdBR iRvAOXFAvd6E1g6AuzyAzDdfuzEf4ApGUZRblgV3Cyt3XDRQh7wpw5yAoyBIP4C9hX8E vCOrWi6shs3oAnzjtz8YPkpgQsvBeMVDguIK+0ir8REXMNeFc5700X2zYtG7HbdEvasx pqBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=zhWAyGJlD9+p8kZdXnATTRilEKnyA/MqjWkRehoUA4I=; b=SYLMIzmpIYysaoQg3gFjijSJ2XVfow/D1FK3g4ZaJh+PGo5wdQ5h5wvHkwWjsrmj3E rzTrSwfcNk2p2+Iali25N/zqOfnhMqeS0gTs4cw4Yb/UJuHLj7ZipLpLaSUXERXVw+Xj HiGR8AYaY7hu5q/EUjVlfxHSpomKMVsbbgRMK72ioapm7Y/Kh/JAwfsmvAHVZTRXlXTS 11pL1iJIXFDLXWuKghEpTy+d8l7peoAGOEqFZhSUk1/xg4topr/TZZAsQKaDnlh+cm6i cQqZ9qIc2izckMSsT+PqvtrLRjhoJFXaddgskRoHLN1RpOApzhLx7vb9mLWTTjQwpV/N 7F/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HkR1xQNw; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id p20sor610086lfg.65.2021.06.09.03.23.39 for (Google Transport Security); Wed, 09 Jun 2021 03:23:39 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HkR1xQNw; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zhWAyGJlD9+p8kZdXnATTRilEKnyA/MqjWkRehoUA4I=; b=HkR1xQNwqeMiqx+NVLs27b5+ZwFRXlx3p4+D5B97yjrlxWl/Gu697qXa4pfLF8m3Zq L+nDmXZJilwnb7BCkllXyOAMQfHJdvC/TkdYe1KHcOUhxXg9iPWy+7Ycu6sZrYlerNtL BCOeGDWhYAjtH1K+4pPYzBFiIoZTb4HXVstVwsv78j6k/kkx34XNMsCePDku14RFSyNr JL/IDgS2JfzBJY78E9v0JPHYsnF9Jyb+QLC+SSOD5vzwllQeBIn9TBhWZ+XlRyT6Jyxd KIcpY1mZUt0ZCegWKGQmnib/Kwj3oQBUjI78m/3lFF+1qML/LZJyNLMir3zGGEuL1mT8 RQTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zhWAyGJlD9+p8kZdXnATTRilEKnyA/MqjWkRehoUA4I=; b=IObcWLvfDDQB0PPuIWZ0HF2LhODPjbkRPTNT9+nnfP8N1f4P6wfXoGZBGMjLSGe3jA GnM1+9G/j9gUm34TRRNo0oJBNhDymMzDOnEaE80fdZNv9bB9ooNsLlNEx8rsp/7Un4DI 2cVOUApP/bBG2V5yNWuwYDHMFEhW32JIrgGrx2gNQ/rulwV10sFuCktaSTB3qu7mYO36 bKm2WQ/VWYjXWmtgQk4UYNmI9p6gqTl6VgDDx/S1kFyk5vB7NY0R93Gc/FueG0XpRHlf 0f0MfoNWofLDikmIqD1YmBvrdtXcKPZPbhpyvBNTt0xNbBfkLybibddUMcCHJd8TfZk2 F25g== X-Gm-Message-State: AOAM5335Qve/ayXP6MjHj0putngdo01jXhPDOCzNMLemFcbuK7I/fvvn JGIac7jps+W1EmwMt+0JljyG1z+Q X-Google-Smtp-Source: ABdhPJxLbMg0oh/XLFRzv20keBFEszS5O6B13TgT112e9SXICx8Tp6ARGbiMsBOoE20bmuQG31c4ng== X-Received: by 2002:a05:6512:3287:: with SMTP id p7mr17886650lfe.213.1623234219190; Wed, 09 Jun 2021 03:23:39 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:38 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 2/7] tee: simplify shm pool handling Date: Wed, 9 Jun 2021 12:23:19 +0200 Message-Id: <20210609102324.2222332-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 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 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. Signed-off-by: Jens Wiklander --- drivers/tee/amdtee/shm_pool.c | 55 ++++++------------ drivers/tee/optee/Kconfig | 8 --- drivers/tee/optee/core.c | 72 +++-------------------- drivers/tee/optee/shm_pool.c | 51 ++++++++++------- drivers/tee/optee/shm_pool.h | 2 +- drivers/tee/tee_private.h | 11 ---- drivers/tee/tee_shm.c | 29 +++++----- drivers/tee/tee_shm_pool.c | 104 +++++++++++----------------------- include/linux/tee_drv.h | 58 +++++++------------ 9 files changed, 120 insertions(+), 270 deletions(-) -- 2.31.1 diff --git a/drivers/tee/amdtee/shm_pool.c b/drivers/tee/amdtee/shm_pool.c index 065854e2db18..81c23f30b455 100644 --- a/drivers/tee/amdtee/shm_pool.c +++ b/drivers/tee/amdtee/shm_pool.c @@ -8,13 +8,17 @@ #include #include "amdtee_private.h" -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, u_int align) { unsigned int order = get_order(size); unsigned long va; int rc; + /* + * Ignore alignment since this is already going to be page aligned + * and there's no need for any larger alignment. + */ va = __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; } -static void pool_op_free(struct tee_shm_pool_mgr *poolm, struct tee_shm *shm) +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 = NULL; } -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); } -static const struct tee_shm_pool_mgr_ops pool_ops = { +static const struct tee_shm_pool_ops pool_ops = { .alloc = pool_op_alloc, .free = pool_op_free, - .destroy_poolmgr = pool_op_destroy_poolmgr, + .destroy_pool = pool_op_destroy_pool, }; -static struct tee_shm_pool_mgr *pool_mem_mgr_alloc(void) -{ - struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); - - if (!mgr) - return ERR_PTR(-ENOMEM); - - mgr->ops = &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 = kzalloc(sizeof(*pool), GFP_KERNEL); - rc = pool_mem_mgr_alloc(); - if (IS_ERR(rc)) - return rc; - priv_mgr = rc; - - rc = pool_mem_mgr_alloc(); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - return rc; - } - dmabuf_mgr = rc; + if (!pool) + return ERR_PTR(-ENOMEM); - rc = 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 = &pool_ops; - 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 ddb8f9ecf307..0c287345f9fe 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -428,33 +428,6 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn, return true; } -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 = optee_shm_pool_alloc_pages(); - if (IS_ERR(rc)) - return rc; - priv_mgr = rc; - - rc = optee_shm_pool_alloc_pages(); - if (IS_ERR(rc)) { - tee_shm_pool_mgr_destroy(priv_mgr); - return rc; - } - dmabuf_mgr = rc; - - rc = 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_shm) { @@ -468,10 +441,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 = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE; invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc); if (res.result.status != OPTEE_SMC_RETURN_OK) { @@ -489,11 +459,6 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm) paddr = begin; size = end - begin; - if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) { - pr_err("too small shared memory area\n"); - return ERR_PTR(-EINVAL); - } - va = memremap(paddr, size, MEMREMAP_WB); if (!va) { pr_err("shared memory ioremap failed\n"); @@ -501,35 +466,13 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm) } vaddr = (unsigned long)va; - rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz, - 3 /* 8 bytes aligned */); - if (IS_ERR(rc)) - goto err_memunmap; - priv_mgr = rc; - - vaddr += sz; - paddr += sz; - size -= sz; - - rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT); + rc = tee_shm_pool_alloc_res_mem(vaddr, paddr, size, + 9 /* 512 bytes aligned */); if (IS_ERR(rc)) - goto err_free_priv_mgr; - dmabuf_mgr = rc; - - rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); - if (IS_ERR(rc)) - goto err_free_dmabuf_mgr; - - *memremaped_shm = va; - - return rc; + memunmap(va); + else + *memremaped_shm = va; -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; } @@ -637,7 +580,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 = optee_config_dyn_shm(); + pool = optee_shm_pool_alloc_pages(); /* * If dynamic shared memory is not available or failed - try static one @@ -712,8 +655,7 @@ static int optee_probe(struct platform_device *pdev) tee_device_unregister(optee->teedev); kfree(optee); } - if (pool) - tee_shm_pool_free(pool); + tee_shm_pool_free(pool); if (memremaped_shm) memunmap(memremaped_shm); return rc; diff --git a/drivers/tee/optee/shm_pool.c b/drivers/tee/optee/shm_pool.c index d767eebf30bd..b036714845c1 100644 --- a/drivers/tee/optee/shm_pool.c +++ b/drivers/tee/optee/shm_pool.c @@ -12,13 +12,17 @@ #include "optee_smc.h" #include "shm_pool.h" -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, u_int align) { unsigned int order = get_order(size); struct page *page; int rc = 0; + /* + * Ignore alignment since this is already going to be page aligned + * and there's no need for any larger alignment. + */ page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); if (!page) return -ENOMEM; @@ -27,47 +31,52 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, shm->paddr = page_to_phys(page); shm->size = PAGE_SIZE << order; - if (shm->flags & TEE_SHM_DMA_BUF) { + if (shm->flags & TEE_SHM_REGISTER) { unsigned int nr_pages = 1 << order, i; struct page **pages; pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL); - if (!pages) - return -ENOMEM; - - for (i = 0; i < nr_pages; i++) { - pages[i] = page; - page++; + if (!pages) { + rc = -ENOMEM; + goto err; } - shm->flags |= TEE_SHM_REGISTER; + for (i = 0; i < nr_pages; i++) + pages[i] = page + i; + rc = optee_shm_register(shm->ctx, shm, pages, nr_pages, (unsigned long)shm->kaddr); kfree(pages); + if (rc) + goto err; } + return 0; + +err: + free_pages((unsigned long)shm->kaddr, order); return rc; } -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_DMA_BUF) + if (shm->flags & TEE_SHM_REGISTER) optee_shm_unregister(shm->ctx, shm); free_pages((unsigned long)shm->kaddr, get_order(shm->size)); shm->kaddr = NULL; } -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); } -static const struct tee_shm_pool_mgr_ops pool_ops = { +static const struct tee_shm_pool_ops pool_ops = { .alloc = pool_op_alloc, .free = pool_op_free, - .destroy_poolmgr = pool_op_destroy_poolmgr, + .destroy_pool = pool_op_destroy_pool, }; /** @@ -76,14 +85,14 @@ static const struct tee_shm_pool_mgr_ops pool_ops = { * This pool is used when OP-TEE supports dymanic SHM. In this case * command buffers and such are allocated from kernel's own memory. */ -struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void) +struct tee_shm_pool *optee_shm_pool_alloc_pages(void) { - struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); + struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL); - if (!mgr) + if (!pool) return ERR_PTR(-ENOMEM); - mgr->ops = &pool_ops; + pool->ops = &pool_ops; - return mgr; + return pool; } diff --git a/drivers/tee/optee/shm_pool.h b/drivers/tee/optee/shm_pool.h index 28109d991c4b..7024b9926ada 100644 --- a/drivers/tee/optee/shm_pool.h +++ b/drivers/tee/optee/shm_pool.h @@ -9,6 +9,6 @@ #include -struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void); +struct tee_shm_pool *optee_shm_pool_alloc_pages(void); #endif diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index e55204df31ce..72376cf38bc0 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -12,17 +12,6 @@ #include #include -/** - * 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 diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 00472f5ce22e..b9dbf4bce149 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -39,14 +39,7 @@ static void tee_shm_release(struct tee_shm *shm) } if (shm->flags & TEE_SHM_POOL) { - struct tee_shm_pool_mgr *poolm; - - if (shm->flags & TEE_SHM_DMA_BUF) - poolm = teedev->pool->dma_buf_mgr; - else - poolm = 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 = teedev->desc->ops->shm_unregister(shm->ctx, shm); @@ -106,8 +99,8 @@ static const struct dma_buf_ops tee_shm_dma_buf_ops = { struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) { struct tee_device *teedev = ctx->teedev; - struct tee_shm_pool_mgr *poolm = NULL; struct tee_shm *shm; + size_t align; void *ret; int rc; @@ -139,12 +132,18 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) shm->flags = flags | TEE_SHM_POOL; shm->ctx = ctx; - if (flags & TEE_SHM_DMA_BUF) - poolm = teedev->pool->dma_buf_mgr; - else - poolm = teedev->pool->private_mgr; + if (flags & TEE_SHM_DMA_BUF) { + align = PAGE_SIZE; + /* + * Request to register the shm in the pool allocator below + * if supported. + */ + shm->flags |= TEE_SHM_REGISTER; + } else { + align = 2 * sizeof(long); + } - rc = poolm->ops->alloc(poolm, shm, size); + rc = teedev->pool->ops->alloc(teedev->pool, shm, size, align); if (rc) { ret = ERR_PTR(rc); goto err_kfree; @@ -184,7 +183,7 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) mutex_unlock(&teedev->mutex); } 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 a9f9d50fd181..5ceab73ff1b5 100644 --- a/drivers/tee/tee_shm_pool.c +++ b/drivers/tee/tee_shm_pool.c @@ -9,14 +9,16 @@ #include #include "tee_private.h" -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 *shm, + size_t size, u_int align) { unsigned long va; - struct gen_pool *genpool = poolm->private_data; - size_t s = roundup(size, 1 << genpool->min_alloc_order); + struct gen_pool *genpool = pool->private_data; + size_t a = max(align, 1U << genpool->min_alloc_order); + struct genpool_data_align data = { .align = a }; + size_t s = roundup(size, a); - va = gen_pool_alloc(genpool, s); + va = gen_pool_alloc_algo(genpool, s, gen_pool_first_fit_align, &data); if (!va) return -ENOMEM; @@ -24,107 +26,67 @@ static int pool_op_gen_alloc(struct tee_shm_pool_mgr *poolm, shm->kaddr = (void *)va; shm->paddr = gen_pool_virt_to_phys(genpool, va); shm->size = 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 &= ~TEE_SHM_REGISTER; return 0; } -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 *shm) { - gen_pool_free(poolm->private_data, (unsigned long)shm->kaddr, + gen_pool_free(pool->private_data, (unsigned long)shm->kaddr, shm->size); shm->kaddr = NULL; } -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); } -static const struct tee_shm_pool_mgr_ops pool_ops_generic = { +static const struct tee_shm_pool_ops pool_ops_generic = { .alloc = pool_op_gen_alloc, .free = pool_op_gen_free, - .destroy_poolmgr = pool_op_gen_destroy_poolmgr, + .destroy_pool = pool_op_gen_destroy_pool, }; -struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr, - 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 = PAGE_SIZE - 1; - struct tee_shm_pool_mgr *mgr; + struct tee_shm_pool *pool; int rc; /* Start and end must be page aligned */ if (vaddr & page_mask || paddr & page_mask || size & page_mask) return ERR_PTR(-EINVAL); - mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); - if (!mgr) + pool = kzalloc(sizeof(*pool), GFP_KERNEL); + if (!pool) return ERR_PTR(-ENOMEM); - mgr->private_data = gen_pool_create(min_alloc_order, -1); - if (!mgr->private_data) { + pool->private_data = gen_pool_create(min_alloc_order, -1); + if (!pool->private_data) { rc = -ENOMEM; goto err; } - gen_pool_set_algo(mgr->private_data, gen_pool_best_fit, NULL); - rc = gen_pool_add_virt(mgr->private_data, vaddr, paddr, size, -1); + rc = 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; } - mgr->ops = &pool_ops_generic; + pool->ops = &pool_ops_generic; - return mgr; + return pool; err: - kfree(mgr); + kfree(pool); 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 = kzalloc(sizeof(*pool), GFP_KERNEL); - if (!pool) - return ERR_PTR(-ENOMEM); - - pool->private_mgr = priv_mgr; - pool->dma_buf_mgr = 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 53b9b2a13a87..62b7c7a55743 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -215,62 +215,39 @@ struct tee_shm { }; /** - * 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; }; /** - * 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, u_int align); + void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm); + void (*destroy_pool)(struct tee_shm_pool *pool); }; -/** - * 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 failure. - */ -struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr, - 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); /** * tee_shm_pool_free() - Free a shared memory pool @@ -279,7 +256,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); +} /** * tee_get_drvdata() - Return driver_data pointer From patchwork Wed Jun 9 10:23:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456816 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115922jae; Wed, 9 Jun 2021 03:23:40 -0700 (PDT) X-Received: by 2002:a2e:5c82:: with SMTP id q124mr21948230ljb.332.1623234220428; Wed, 09 Jun 2021 03:23:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234220; cv=none; d=google.com; s=arc-20160816; b=ubQzO0BZH6gE/TptIVhpFmGi9g9bf+FnBpzxHl3tenDEVmCttHNmTc13qCqO4hBfcp CCTr+V8Ti0zlde2yRDsI91l4OS0VpF4GltYya5bBF0OAK86jA7IbzTdOy4pTq49OBa/t nj4cbMyfIUlrFbnmF6SgRRHQrGGD6GPmnvRdbu+PStLcK6Hxnz/azMvKCvRpl4VIo2aQ 4fLUeci7P30KaGQBStSVqneLSAXLGSWDCY3GwybokFuTzZrd4pF5coU5ycrAzDqTPIYs AybphYcnfhw2QOqlu9DP+TrDcv9H9K0Feh2BvfIHt7vi5nYAndjXfQObNQ+gSWVLeUPq Md+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=7RcDq8BcfTAWvH1WjeQwKK7cYkn1EkqelTe3jQATvG4=; b=roKpYIu40HqwbYiNmplLV6OWNdcmIxX0mIL/9a+vO5JqPwnckBzBYjkBRaAmW5NA9G Y5UGhdtb7EoZS/xiYKgyJ2o8ATGd1m8zTh2nrqBMI5FeB5JCQtXjYugNwPm1CJchh7LU miaXUuHzZe7jr5C1tN/lxOwz5pP4czB+APDJAKzlJKc0PPOiZx4LOFISozPW9T1cX2kt YWNX7S3CEXxQdAlm5z024xfqoc/+aX+umJP1jFjw8QAfxFZ3HHIXQAwL8f/+CzrjHDBP mORguiZKYnBIIQqOftqvOT9NJo7EFd2tkKK8z0b2s5oC1MVSH6ki+jQGR/pMdsNFbH9F eg4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ozpM35nW; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id q20sor338525ljg.20.2021.06.09.03.23.40 for (Google Transport Security); Wed, 09 Jun 2021 03:23:40 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ozpM35nW; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7RcDq8BcfTAWvH1WjeQwKK7cYkn1EkqelTe3jQATvG4=; b=ozpM35nWXWBjw5iUr59+oW0hcacsvLGFrjStft3HGELn8/8ScTMkxs4XPjEmtlo/BN ZUyvZD+GH0yxF8V9U8dQnKU8fPdl+wun5GkDI0U2GJt0Xv92w9xAo9PWaAi0VHA8PXW+ I6A6fYa98bjoZ563pMDdIzgn/Ta76RMVZQ7T4kINmT1u2SAYW/SkhEF09H1UI6SF+QTY w0P85B/IPraoS/NZEmjn8As+YnIg7xiqoOyRilivQ3E7U9qsItvotMr6vqzUXe2p72Dx ZuZLGwsyOTZMi+GOI0cDJeYJHZLoTWlt7/ldmiprb7nEoIfghaB/w77zPbo9PDbEpG69 aRwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7RcDq8BcfTAWvH1WjeQwKK7cYkn1EkqelTe3jQATvG4=; b=J3i8YSnCHG2Pa2O9hMxdxsISFce8DykRpnEPzdgQ3O7UCFIppIZ5ZYf2620/Sbx/Ic TLapWuSdcMcSxZ7sp3RS7uj68xWU9zC9kzQCyA3GerksIFMezCz34A4+EnCtLcjrw8H7 a4kQ+kOisKSgCdSDr+L7oabsoOYPVlMn8vMbPE9j7YMVJbm3W//xKw1936Tr5z05K6bx PKJG+cL9GVhMXpADdcB4YiTZKyegS6mWiEtlFWFrxZLEj23gQWuFU2sEkQbS4xmy/9vL tsUOYWXwBCBcKJutiA6Q3xpH8u+ZPTGrIzFaSY2SDBkz4uXAlD5/3OfKVMTg0W8ciqxs V0Mw== X-Gm-Message-State: AOAM5301t5dNfM2iD8IKTiczV4Ncih3L536xYF/AXwHjDIbtCch/89qt 4ZdCnDn99up//yBKMWWjXzkYuY3+ X-Google-Smtp-Source: ABdhPJzF0zg4WbIXADx/FTVUlCvDWQq7mIP7AFvOez86MvdSJywFf7MGKKUVQv0m6jWAIfrQN8y1wA== X-Received: by 2002:a2e:9c08:: with SMTP id s8mr22318777lji.64.1623234220060; Wed, 09 Jun 2021 03:23:40 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:39 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 3/7] tee: add tee_shm_alloc_kernel_buf() Date: Wed, 9 Jun 2021 12:23:20 +0200 Message-Id: <20210609102324.2222332-4-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Adds a new function tee_shm_alloc_kernel_buf() to allocate shared memory from a kernel driver. This function can later be made more lightweight by unnecessary dma-buf export. Signed-off-by: Jens Wiklander --- drivers/tee/tee_shm.c | 18 ++++++++++++++++++ include/linux/tee_drv.h | 1 + 2 files changed, 19 insertions(+) -- 2.31.1 diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index b9dbf4bce149..63fce8d39d8b 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -192,6 +192,24 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) } EXPORT_SYMBOL_GPL(tee_shm_alloc); +/** + * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer + * @ctx: Context that allocates the shared memory + * @size: Requested size of shared memory + * + * The returned memory registered in secure world and is suitable to be + * passed as a memory buffer in parameter argument to + * tee_client_invoke_func(). The memory allocated is later freed with a + * call to tee_shm_free(). + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size) +{ + return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); +} +EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); + struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, size_t length, u32 flags) { diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 62b7c7a55743..58b319766f8e 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -282,6 +282,7 @@ void *tee_get_drvdata(struct tee_device *teedev); * @returns a pointer to 'struct tee_shm' */ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags); +struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size); /** * tee_shm_register() - Register shared memory buffer From patchwork Wed Jun 9 10:23:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456817 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115929jae; Wed, 9 Jun 2021 03:23:41 -0700 (PDT) X-Received: by 2002:a05:6512:3084:: with SMTP id z4mr13377128lfd.128.1623234221197; Wed, 09 Jun 2021 03:23:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234221; cv=none; d=google.com; s=arc-20160816; b=LCBxMA/6f3iJ7Rb0iDeXB+Nw6sKgjHUPvlr5JAUqbdC9ZXg8oaCmRl9hk+aYq+x9ee eZXqVHexIW7AUQhyj9AFMqcBJq8UpH8a0wSDr+bAIbbZDsnJvLkFYTYb2EQhtwSJWAR5 qImdCevAiXISr6tn3zx8jQue8gOb5CoGRf0EdBfsCYCqBi8s2TK56RKJzqHCjXWvwE/B sOJylGjWUP10j9UO281ofD1lGEcwzkhJUO4gZTF5oKC7dkCWTuy1jAy3tcPpcqDcm/3y KXVONhGkjzm2a/Uh4RwOt6DqdfPYhnnse0h5hAHmFvuWrmelHYgOW5L7djj4/jF5U8EO HlOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=iBza1mKhlqsg6etHF8D7TfcQzqGCBqmRr7/AGAencgU=; b=vNiq4Dk8h78roYW2lSnXBkgtEUpjzuBHtQuxoF61CsGjUL6VTGNJH7JH0ksxPZKe9m 6ni6mf/uGygHjinlXQn+9qCHCL7BFCPxQSsIU82puUXeXN5eb22kMqENiR7UGAbvXuaU Pla6WcYFwsylW7j+bf45Hk2Bw4IiMs3Zv6OJVibYlDOq2Omn1N/y+RPAl0qJJ0Z6H2wX qGSQOHR6a5TOyU7tGu3V5I5Av6CGWQf+w6idMbXhFVt1x1NoCXydOvUvYkU6MDWn3P1I 5gu5M+gwZgVTp0Rgnm9Pi+1QEW8ws2yExGHF43xeyTgqc7B7vWMuDQQQ36qBM0uuqkRe ubLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IABFa0bj; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id i3sor1150275lji.70.2021.06.09.03.23.41 for (Google Transport Security); Wed, 09 Jun 2021 03:23:41 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IABFa0bj; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iBza1mKhlqsg6etHF8D7TfcQzqGCBqmRr7/AGAencgU=; b=IABFa0bjwccpp/bNIj/Ur6wWmjvpwTQ4uvwHXETm960Yor7fI7C0+IUiKkqzhdMHYt iIbNcwWw7mSP3CTqJM5WCz9e1fVOkqqyJ6IbJ7vb4F0AOABdAqzfgZynRLN+8RPYb3s4 i8P1Mbi9AiOyCUoNtiLGY1WaWlwAMtRFzd33qTh+ik4miJF8Nyfg1ll+Pyr+6KOk03+/ NxPSZaHEVcrddgn2hJqbpig76yiGdjbrffGibYYLE58P0dgOSFhz0/bgc7JeNwIYwxn1 ULxNvboBP4WgP5Ou48sQjvhcfGoKYsGZbxYJsszS0eBdqj3Y1FLFX8d/tR8aJ7iJCT1P lXFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iBza1mKhlqsg6etHF8D7TfcQzqGCBqmRr7/AGAencgU=; b=gtO5ytRKjy8jP/7js9tVnGP/V13p+iiewU/0hKA4A6WwztFB8Zt4UteIYsuMEW8OjJ 1eHUoycIixbVLAv56ncsy+dg+wKKTURAIax+27T8Om/HenQrYYSuPUBV/800faHM2tG2 d2KlmUoBOwhWiLuRsvipm32NW6hY+yOBlfgGZSiO0+YytQP9z+CSa+vooZzuOanUTI9o 9iJWgKUhQnWEUCsKUPUwtdTUWIg8r/jr7L9yiAsl7z9DXovP4YVfyDLkkY6nldIlx43q 48M7keu7souoT6w/9pAjnRAYiBg1EfwuoNTNwmyV+D9HJrwc1wL79Hu4X2I7gnYE5FC+ Ujtw== X-Gm-Message-State: AOAM532NvK+32RvcikMUbGrIzvu+DYSXH+2AvTzfGv3gQmZMPJaaIxj4 u8x9kHJX6FHHKMKkSu2QWHDbT9L8 X-Google-Smtp-Source: ABdhPJxJ/g/8K2URkWfxiXdN18FhFhzSSYRq1MrmDLL0fZZa78RlccOZrwenL4lGTKNI4NcOB4D9Nw== X-Received: by 2002:a2e:914d:: with SMTP id q13mr6919196ljg.179.1623234220818; Wed, 09 Jun 2021 03:23:40 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:40 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 4/7] hwrng: optee-rng: use tee_shm_alloc_kernel_buf() Date: Wed, 9 Jun 2021 12:23:21 +0200 Message-Id: <20210609102324.2222332-5-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 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. Signed-off-by: Jens Wiklander --- drivers/char/hw_random/optee-rng.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) -- 2.31.1 diff --git a/drivers/char/hw_random/optee-rng.c b/drivers/char/hw_random/optee-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 = to_optee_rng_private(rng); struct tee_shm *entropy_shm_pool = NULL; - entropy_shm_pool = tee_shm_alloc(pvt_data->ctx, MAX_ENTROPY_REQ_SZ, - TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + entropy_shm_pool = 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); } From patchwork Wed Jun 9 10:23:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456818 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115940jae; Wed, 9 Jun 2021 03:23:42 -0700 (PDT) X-Received: by 2002:a19:24d4:: with SMTP id k203mr17974030lfk.271.1623234221933; Wed, 09 Jun 2021 03:23:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234221; cv=none; d=google.com; s=arc-20160816; b=KOJI3Dv8Evc6rcLo+AfED6cxUcoGuh7mAk+lE7yiTff3Wp/ZYIuCriZhpKW2pWPMoN skAxlnHGedlHnnDN++hOXvN1O6TVqerAywbW6L9ygwVNEd1LPh57uUgVU0f+yenxIo0f 2bwisneFUBIuCHzUw++8K2ij2Bs2DDRDQNCRkO+CSz8QaqteVg4PuB/+MGYTnVke9r0M nPkno8Ocyysh2kPKNN46E6aurADRUQM7wCH/Zps8jgemaOG14qJ5PbkyPxfpsqu5rduH TaSWBW3VZZxUE+kwKRhGlljXHHvbSMOlNT1OsbNaLJsAkYS0Rcc8dD0mlKtpBJ2bG6Ik ZV2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=rSIr/zgD0QKUjYJIhEJ6XI1VLo+32XT0J4ID1IoyWD4=; b=d6TzOWHrHUfnmsqsgaN13zoWV5u+lXgcDH4tPKyRDpBNp7Olz9mOTDlzoeAbNPxM/6 e01dlvsaQUAsJDVin8gRBrjfM5Mee+5VVlS+gYcrREkzrfNfKJJ+mc4SGMtNvoV5pidc E7V+tZ/A3SIt4GJjaIzdCAmacFgQ3PK/AWhjc9uEfRkTv7tcHnV0T+qM36QQpn68vOqk wSINzNPcw2DPPpSuDyb4jg0scNOqwaw2fz4ArEzwPc6TSK390WCXv/0byyprxUUIbwaJ L88oy2+luijychWlppxsxO+pHQ3IZ/IwCJoNVwLantBdjZxH9tn5T4wuHSbOX1g+QQxZ pUiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fqAD6isZ; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id a16sor1112100lji.63.2021.06.09.03.23.41 for (Google Transport Security); Wed, 09 Jun 2021 03:23:41 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fqAD6isZ; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rSIr/zgD0QKUjYJIhEJ6XI1VLo+32XT0J4ID1IoyWD4=; b=fqAD6isZzOpdAF9uAsnzbY6yrKqxk+H25yKbeNb+ngubcNcT0kFOdS+WdP63PHHUxC HmbNlTSi/IukvVImU0SQ0hRYfsU7yaQJLtUvzSHrwZ0Y76HCXjKyn5yYlU2/MUumCPF9 9yi1u57wsD0zu6bpxQ8wDnxKEWL44I8LVjqcf1pBBa9ABKrCbo7Qhopx41SgKr/WdwkX gb/v8WSzmih9Gs954Py1V0YsjmVOH+nZR8Nc1tkBdN+y+JWwfsvBgCU6cWA/mBIrDt/p emaGFZFxeNq9knd/y1BAHZfbpwU+TH6k35FucKISH2vnlRbwQrIy4SpUNkrrzEFUSDo1 0L/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rSIr/zgD0QKUjYJIhEJ6XI1VLo+32XT0J4ID1IoyWD4=; b=Qymvl8KTW02p7EcFE04vkodyCaFZRz0oD0soI5MSxHcMYfoxREXE3MGUcPblEkWpag cOBtxXMFLtnuYzh0KgZ/o/2th1srZdHrmE9yQ6Y1hM3WPUQ2+mA80XxZSnqC3DIcBlSp ZqbjWxM3FG05FIsPjs18nlybTX0saBgd1QOjEtSwf39r2VxUDD+DlJA0ekaFOjH5NT6e jJfTraP7Dkun3mBFqjyoonQbmyQxkqfGIjncneLbyZrGhtTdk78KJGN59OKfzy8M78U8 L8dXZCm/ycHOvjSnNAj+oR0EEuU5cpp+9v8voRABqs1TRyzide2cUaPJvozAdkgDdpTK gawg== X-Gm-Message-State: AOAM533n6b/g8rBEt/8NBFv/B5jr3pqCEskb67aYCCW+Kn4MGWLlt1Ue 9htomVYWyTTzlUdmnZphkGfUD1xUVaM3A9h+dzg= X-Google-Smtp-Source: ABdhPJxRpAglWaXtGp3R3fiw+lgOGV4wZw8PB/hRjO196/Gjs3ftPKT5ETRh0FArSEmyNuoX6BcBEg== X-Received: by 2002:a05:651c:1185:: with SMTP id w5mr21510897ljo.379.1623234221556; Wed, 09 Jun 2021 03:23:41 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:41 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 5/7] tpm_ftpm_tee: use tee_shm_alloc_kernel_buf() Date: Wed, 9 Jun 2021 12:23:22 +0200 Message-Id: <20210609102324.2222332-6-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 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. Signed-off-by: Jens Wiklander --- drivers/char/tpm/tpm_ftpm_tee.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) -- 2.31.1 diff --git a/drivers/char/tpm/tpm_ftpm_tee.c b/drivers/char/tpm/tpm_ftpm_tee.c index 2ccdf8ac6994..6e3235565a4d 100644 --- a/drivers/char/tpm/tpm_ftpm_tee.c +++ b/drivers/char/tpm/tpm_ftpm_tee.c @@ -254,11 +254,11 @@ static int ftpm_tee_probe(struct device *dev) pvt_data->session = sess_arg.session; /* Allocate dynamic shared memory with fTPM TA */ - pvt_data->shm = tee_shm_alloc(pvt_data->ctx, - MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE, - TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + pvt_data->shm = tee_shm_alloc_kernel_buf(pvt_data->ctx, + MAX_COMMAND_SIZE + + MAX_RESPONSE_SIZE); if (IS_ERR(pvt_data->shm)) { - dev_err(dev, "%s: tee_shm_alloc failed\n", __func__); + dev_err(dev, "%s: tee_shm_alloc_kernel_buf failed\n", __func__); rc = -ENOMEM; goto out_shm_alloc; } From patchwork Wed Jun 9 10:23:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456819 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115955jae; Wed, 9 Jun 2021 03:23:42 -0700 (PDT) X-Received: by 2002:a05:6512:22c3:: with SMTP id g3mr19128229lfu.495.1623234222795; Wed, 09 Jun 2021 03:23:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234222; cv=none; d=google.com; s=arc-20160816; b=TCAT0ukSiqRIH7fqxUaq5sT7pWh47XfUDiWE+eQNgXYERTRGiF8jfN8LrhPmcQH+Qy VZygXJJKbew2aDDDoaqUTWZCGXynbGn4v0d6x5RZu6dRBqaZ/MmcG9+o6kuJzeGtVdNa AgpTBFxQjw4pn3U0KMsKnoOEyNcHpao+235SfOh5A5j/Ms8cuRXIQExsSPpKNSTkcL8j MFznfObd3KBIaPc+6tWHO0vLTDRC+uiqn2ZIWJ4ikkW/NZ3N7sqdLFxazEOwqfF8FAs+ csYy+mdh2msy3TGRo8Bxdz3wrrqcAv9pg1U0mrcumrVNIU6OPaZuH/V9Ov48Cbw0vM30 dabA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=z41Cr+HEWGY0CsHaBd8pm3xNtEWwWXvXTvPBLBMGN/o=; b=W3I0VBR9zO7PGaUvPPlwRx6tcH11AFLiWSjViKH0TyvFGj11u0Bfuk0O0zwtfNoVfM XJSiJi7aDpPaHnuvZ0WTWQWcfKQatzTpqXT4YlTHmTnW41P93Q6bZr6SRmmt7L+lmYHm nDnq+6GF7yXHgF5Mb35SdAxLbXfkBmeRtQp8KxxnFOw0yl612fiqQp5bsv83MBoatkiy sJmB2ZQqMxveNEozuAIWyO69inYn4j9/xgYrvzPDVewTA4H6Q412btfSsV5Pq5VcVFPH 0Ijb5VeWproiFsWQ4OCOfVJ1ZDEF2Umoj9SR7lv0Bp6NsPDwQrbPCa+Em1IVz18DXUCd Z2Tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wxnDktWn; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id r13sor1154264lji.29.2021.06.09.03.23.42 for (Google Transport Security); Wed, 09 Jun 2021 03:23:42 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wxnDktWn; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=z41Cr+HEWGY0CsHaBd8pm3xNtEWwWXvXTvPBLBMGN/o=; b=wxnDktWnokbe9vdca0sig349xZwGFRxYuFMA+W0zn7C/YzUSGShW9nf9GwP+zBWewr KcIhMUvVU7R6230pLSzbrXrkFRT/Krbs6bdsqTll3nVZEW43N6ica7F+OuHFDn1OihUc hBhJPbDM0gVwJ1TfOFHiHSuSlWi2MY5Ve5RS1pVhKTTTTnrxXlZGZRLcToK4DORu19mq TPhX6S+52cWPuBj74jpI+I2yLXiv8faJT7DJz0Isu5f+C3H4wfp6hr/sqtLAOxm621j0 yRad3i1pPSmuIGfc5oOhDfFuQZGEpYj3jjjJ0TUkjmlv8E8CK6Zso99mhY4Nk9tmdoPB vDZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z41Cr+HEWGY0CsHaBd8pm3xNtEWwWXvXTvPBLBMGN/o=; b=JLO35luOATdeNC5K95roAaXIoDzqlu+zsSzw5loNVuc5pKBhGsiEp2z6gUbg1/l37v WVfH09i/5QflYrsfb/qvKsV4bUbTBFIVZeLXPNes4Mh4A1cXxPBkCPZBjp1ObRQ+LdAm JsPoWWtbWpejLqy00k06Tt9PYFJe4nm0nhX4VPsC/7OWkmAyAMs8jfHzodoRJc+4bMaW 9FwNChQkz335V7A8ZdpfvmrK2yWs/uMhi6tCX37pOe60c1DIugAqGUVyqfn4rmTfWOJk mFgwVDEHtx5h6DkEDC6w+CCBpzbUGhePQ4yMLsA7yGPPFUa0frmvqdbj3mM6Cy/dFL0g ZDnQ== X-Gm-Message-State: AOAM530O1CGjKtAYzdn0Ds9FTGYWroliQc5o12DOIDltw3Iu274D4p3Q 2T+Louao2tknoqwcY6yfZ0oyzX4h X-Google-Smtp-Source: ABdhPJzypUn77FFisUbpoXFQUpt73CwKxfl7HpnfNmeGK3YRsOitn1ppydvc0cplYjzX2lC2zkT2mw== X-Received: by 2002:a2e:bc1b:: with SMTP id b27mr4023293ljf.65.1623234222427; Wed, 09 Jun 2021 03:23:42 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:42 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 6/7] firmware: tee_bnxt: use tee_shm_alloc_kernel_buf() Date: Wed, 9 Jun 2021 12:23:23 +0200 Message-Id: <20210609102324.2222332-7-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 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. Signed-off-by: Jens Wiklander --- drivers/firmware/broadcom/tee_bnxt_fw.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) -- 2.31.1 diff --git a/drivers/firmware/broadcom/tee_bnxt_fw.c b/drivers/firmware/broadcom/tee_bnxt_fw.c index ed10da5313e8..56d00ddd4357 100644 --- a/drivers/firmware/broadcom/tee_bnxt_fw.c +++ b/drivers/firmware/broadcom/tee_bnxt_fw.c @@ -212,10 +212,9 @@ static int tee_bnxt_fw_probe(struct device *dev) pvt_data.dev = dev; - fw_shm_pool = tee_shm_alloc(pvt_data.ctx, MAX_SHM_MEM_SZ, - TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + fw_shm_pool = tee_shm_alloc_kernel_buf(pvt_data.ctx, MAX_SHM_MEM_SZ); if (IS_ERR(fw_shm_pool)) { - dev_err(pvt_data.dev, "tee_shm_alloc failed\n"); + dev_err(pvt_data.dev, "tee_shm_alloc_kernel_buf failed\n"); err = PTR_ERR(fw_shm_pool); goto out_sess; } From patchwork Wed Jun 9 10:23:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 456820 Delivered-To: patches@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp5115969jae; Wed, 9 Jun 2021 03:23:43 -0700 (PDT) X-Received: by 2002:ac2:57c1:: with SMTP id k1mr13690427lfo.231.1623234223687; Wed, 09 Jun 2021 03:23:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623234223; cv=none; d=google.com; s=arc-20160816; b=fMTCTPjE2LOOT0BDoZlilm18YD+n+EeKGJJ0f86ZC4RsORmdEf+n+GcC+PTI7nueqQ ZSMZhTvLLqBFQqnPMJHuYi/xicfwuXqmfC0HVGeh4bjo87g3WgGV209uBgoUYA9rdtW5 uz2aOk2TyNpITDke2GULuHPZDG8gafeUvXU5+/48j/w0otbF5/ktefQM1+IccqIdYTeR jqsuNqbLdV/PlK00iQHsj25qv82GFvuEDQNayVmAlppN+BpNECmbDib9n9YkWTZK7/vr iDqBb7+YqyBPTRy05apz8JObaCxfl2tq4zdXaKQeB3kg3qmIXfwB+vzBLCZ1NyLyfHtK PeiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=PBT8nO2aAj1TcAC68otesDJ8QD5R5OjmqyuaSNNlTjQ=; b=zPNhk2HfIohSENVoCUf9K0b192hRyQKg/gfI1/sh19m8Iy+tM8F+Np8WX7QPvX8WN6 0xhK0Tc2XkfOaVQJ4uCcpSD3YLiO10WqoEbv7AfXfMIov5NXMm8Dolvh6frNUwGdb6qf o3qCzk5YUeN3YZQWMxtu85J05mPWdxiTUeMbpdj2z4X0oIVXyC1S5r5BGqbyJyteDsTa yCdqCjM/wTb3+oRJDmuh9Ya0fwHBYI5SRhm6gCVROZfLM7PGCrPwLXR8xqMG+Thjcimq 3meHjP2K5WBSR5tS6wM0cjHJPJqJejIDkV6X+BQZupnhA4m1lBCyvNzA4KfigdONbGJK s04w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZqMGnMn4; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id i18sor1119667ljn.14.2021.06.09.03.23.43 for (Google Transport Security); Wed, 09 Jun 2021 03:23:43 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZqMGnMn4; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PBT8nO2aAj1TcAC68otesDJ8QD5R5OjmqyuaSNNlTjQ=; b=ZqMGnMn4+BnWUdl0+vO+PdmbwDU+m/Lo4ZmW8n9d9qOj/Zz6T8gpyD6npyW0pyzz93 ZAKOw+c8XI8Mn/MMZL6M4TdTO1T5cEv9Cyd2EUHPprokpJE4yTC/dufSkPLm93g2fAGN TCbau8wXt9Jn+DjThfjg47VJjZ9DQ31fF+xFIkKycHEQetfzqLuinedEIzS6bjw1mKKF n9f7vahvJQludgFsD9V+VE3K98oEhJOjsz7dEe67H/9rPe9k7exc8zzUCcc0mbSqVKCX W628O2Rn+3syUSLQC4WeYxogURhJHZLQbwW/aFG08trzFbxHAPSjHFAvaDwHUtLdQNq2 dvGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PBT8nO2aAj1TcAC68otesDJ8QD5R5OjmqyuaSNNlTjQ=; b=fAFKBeVvPr/9pmV9DUPIUrmsY+qorcV/ia4xyiQGSIql9gLM6NktaSDkixDOUxiMKN H+vtNiD/H80gfof6XgCKQqgkRXGAcnf10CHy52SXPVCofzM158GDU8cENBABk9P8Oa2r 1x4X9SCc4jsQObTpI+GHJeKA7KWbpcOcBHdQen5woQUk8weh1FiF6rLsk85nZ1yDexBe s8lfyGeU7ABbuz6ODT4eMCGoEW7yKsiILUPJQmqQ+HprmlcNJe0/XoJhV9fTtGZLukwy 9Ybu6Zw/s/hDfdu0Q9JC/owrmB7XxXcmRiE5kB7sfWsnyu+/HQVBuhi7LDL8qYcDDXZu bL0Q== X-Gm-Message-State: AOAM530zGx2zE0g+q2TLQlUYcMM6BRN5Yk5wkD9akTVjV9bn5v+3SvN9 J6DcPtPCfnFvTH7O31kHdiayV87J X-Google-Smtp-Source: ABdhPJwA9fOEMt/SRSaDFdPoNfdooQbxe15lSLWjjQzMKranGykXKbndqW7vOxyLiaDtRgpuhtbSbg== X-Received: by 2002:a2e:a4c9:: with SMTP id p9mr21324857ljm.412.1623234223205; Wed, 09 Jun 2021 03:23:43 -0700 (PDT) Return-Path: Received: from jade.urgonet (h-79-136-85-3.A175.priv.bahnhof.se. [79.136.85.3]) by smtp.gmail.com with ESMTPSA id m4sm302966ljp.9.2021.06.09.03.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 03:23:42 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, op-tee@lists.trustedfirmware.org Cc: Sumit Garg , Herbert Xu , Sakkinen , Sasha Levin , Thirupathaiah Annapureddy , Vikas Gupta , "David S . Miller" , Tyler Hicks , Jens Wiklander Subject: [PATCH 7/7] tee: replace tee_shm_alloc() Date: Wed, 9 Jun 2021 12:23:24 +0200 Message-Id: <20210609102324.2222332-8-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210609102324.2222332-1-jens.wiklander@linaro.org> References: <20210609102324.2222332-1-jens.wiklander@linaro.org> MIME-Version: 1.0 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 since we now can avoid using the dma-buf registration. tee_shm_alloc_anon_kernel_buf() - for TEE driver internal use only allowing decoupling a shared memory object from its original tee_context. 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. Signed-off-by: Jens Wiklander --- drivers/tee/optee/call.c | 16 ++-- drivers/tee/optee/core.c | 4 +- drivers/tee/optee/device.c | 5 +- drivers/tee/optee/rpc.c | 8 +- drivers/tee/tee_core.c | 2 +- drivers/tee/tee_shm.c | 186 +++++++++++++++++++++++++++---------- include/linux/tee_drv.h | 19 +--- 7 files changed, 156 insertions(+), 84 deletions(-) -- 2.31.1 diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index 6132cc8d014c..f31257649c0e 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -183,8 +183,8 @@ static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params, struct tee_shm *shm; struct optee_msg_arg *ma; - shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params), - TEE_SHM_MAPPED); + shm = tee_shm_alloc_anon_kernel_buf(ctx, + OPTEE_MSG_GET_ARG_SIZE(num_params)); if (IS_ERR(shm)) return shm; @@ -281,7 +281,7 @@ int optee_open_session(struct tee_context *ctx, arg->ret_origin = msg_arg->ret_origin; } out: - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); return rc; } @@ -312,7 +312,7 @@ int optee_close_session(struct tee_context *ctx, u32 session) msg_arg->session = session; optee_do_call_with_arg(ctx, msg_parg); - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); return 0; } @@ -358,7 +358,7 @@ int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, arg->ret = msg_arg->ret; arg->ret_origin = msg_arg->ret_origin; out: - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); return rc; } @@ -386,7 +386,7 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session) msg_arg->cancel_id = cancel_id; optee_do_call_with_arg(ctx, msg_parg); - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); return 0; } @@ -625,7 +625,7 @@ int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm, msg_arg->ret != TEEC_SUCCESS) rc = -EINVAL; - tee_shm_free(shm_arg); + tee_shm_free_anon_kernel_buf(ctx, shm_arg); out: optee_free_pages_list(pages_list, num_pages); return rc; @@ -650,7 +650,7 @@ int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm) if (optee_do_call_with_arg(ctx, msg_parg) || msg_arg->ret != TEEC_SUCCESS) rc = -EINVAL; - tee_shm_free(shm_arg); + tee_shm_free_anon_kernel_buf(ctx, shm_arg); return rc; } diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index 0c287345f9fe..a15dc3881636 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -277,7 +277,7 @@ static void optee_release(struct tee_context *ctx) if (!ctxdata) return; - shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED); + shm = tee_shm_alloc_anon_kernel_buf(ctx, sizeof(struct optee_msg_arg)); if (!IS_ERR(shm)) { arg = tee_shm_get_va(shm, 0); /* @@ -305,7 +305,7 @@ static void optee_release(struct tee_context *ctx) kfree(ctxdata); if (!IS_ERR(shm)) - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); ctx->data = NULL; diff --git a/drivers/tee/optee/device.c b/drivers/tee/optee/device.c index ec1d24693eba..5a5bf86b1b95 100644 --- a/drivers/tee/optee/device.c +++ b/drivers/tee/optee/device.c @@ -113,10 +113,9 @@ static int __optee_enumerate_devices(u32 func) if (rc < 0 || !shm_size) goto out_sess; - device_shm = tee_shm_alloc(ctx, shm_size, - TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + device_shm = 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 = PTR_ERR(device_shm); goto out_sess; } diff --git a/drivers/tee/optee/rpc.c b/drivers/tee/optee/rpc.c index 1849180b0278..9108aedb3eee 100644 --- a/drivers/tee/optee/rpc.c +++ b/drivers/tee/optee/rpc.c @@ -314,7 +314,7 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx, shm = cmd_alloc_suppl(ctx, sz); break; case OPTEE_RPC_SHM_TYPE_KERNEL: - shm = tee_shm_alloc(ctx, sz, TEE_SHM_MAPPED); + shm = tee_shm_alloc_anon_kernel_buf(ctx, sz); break; default: arg->ret = TEEC_ERROR_BAD_PARAMETERS; @@ -424,7 +424,7 @@ static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx, cmd_free_suppl(ctx, shm); break; case OPTEE_RPC_SHM_TYPE_KERNEL: - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); break; default: arg->ret = TEEC_ERROR_BAD_PARAMETERS; @@ -502,7 +502,7 @@ void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param, switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) { case OPTEE_SMC_RPC_FUNC_ALLOC: - shm = tee_shm_alloc(ctx, param->a1, TEE_SHM_MAPPED); + shm = tee_shm_alloc_anon_kernel_buf(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, @@ -516,7 +516,7 @@ void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param, break; case OPTEE_SMC_RPC_FUNC_FREE: shm = reg_pair_to_ptr(param->a1, param->a2); - tee_shm_free(shm); + tee_shm_free_anon_kernel_buf(ctx, shm); break; case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR: /* diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 480d294a23ab..4f5c7c17a434 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -293,7 +293,7 @@ static int tee_ioctl_shm_alloc(struct tee_context *ctx, if (data.flags) return -EINVAL; - shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + shm = tee_shm_alloc_user_buf(ctx, data.size); if (IS_ERR(shm)) return PTR_ERR(shm); diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 63fce8d39d8b..d134e2778a3a 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -96,25 +96,14 @@ static const struct dma_buf_ops tee_shm_dma_buf_ops = { .mmap = tee_shm_op_mmap, }; -struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) +static struct tee_shm *shm_alloc_helper(struct tee_context *ctx, size_t size, + size_t align, u32 flags) { struct tee_device *teedev = ctx->teedev; struct tee_shm *shm; - size_t align; void *ret; int rc; - 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))) { - dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags); - return ERR_PTR(-EINVAL); - } - if (!tee_device_get(teedev)) return ERR_PTR(-EINVAL); @@ -131,17 +120,14 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) } shm->flags = flags | TEE_SHM_POOL; + + /* + * 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 = ctx; - if (flags & TEE_SHM_DMA_BUF) { - align = PAGE_SIZE; - /* - * Request to register the shm in the pool allocator below - * if supported. - */ - shm->flags |= TEE_SHM_REGISTER; - } else { - align = 2 * sizeof(long); - } rc = teedev->pool->ops->alloc(teedev->pool, shm, size, align); if (rc) { @@ -149,48 +135,71 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) goto err_kfree; } + return shm; +err_kfree: + kfree(shm); +err_dev_put: + tee_device_put(teedev); + return ret; +} - if (flags & TEE_SHM_DMA_BUF) { - DEFINE_DMA_BUF_EXPORT_INFO(exp_info); +/** + * 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 size) +{ + u32 flags = TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_REGISTER; + struct tee_device *teedev = ctx->teedev; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct tee_shm *shm; + void *ret; - mutex_lock(&teedev->mutex); - shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); - mutex_unlock(&teedev->mutex); - if (shm->id < 0) { - ret = ERR_PTR(shm->id); - goto err_pool_free; - } + shm = shm_alloc_helper(ctx, size, PAGE_SIZE, flags); + if (IS_ERR(shm)) + return shm; - exp_info.ops = &tee_shm_dma_buf_ops; - exp_info.size = shm->size; - exp_info.flags = O_RDWR; - exp_info.priv = shm; + mutex_lock(&teedev->mutex); + shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); + mutex_unlock(&teedev->mutex); + if (shm->id < 0) { + ret = ERR_PTR(shm->id); + goto err_pool_free; + } - shm->dmabuf = dma_buf_export(&exp_info); - if (IS_ERR(shm->dmabuf)) { - ret = ERR_CAST(shm->dmabuf); - goto err_rem; - } + exp_info.ops = &tee_shm_dma_buf_ops; + exp_info.size = shm->size; + exp_info.flags = O_RDWR; + exp_info.priv = shm; + + shm->dmabuf = dma_buf_export(&exp_info); + if (IS_ERR(shm->dmabuf)) { + ret = ERR_CAST(shm->dmabuf); + goto err_rem; } teedev_ctx_get(ctx); - return shm; err_rem: - if (flags & TEE_SHM_DMA_BUF) { - mutex_lock(&teedev->mutex); - idr_remove(&teedev->idr, shm->id); - mutex_unlock(&teedev->mutex); - } + mutex_lock(&teedev->mutex); + idr_remove(&teedev->idr, shm->id); + mutex_unlock(&teedev->mutex); 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); +EXPORT_SYMBOL_GPL(tee_shm_alloc_user_buf); /** * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer @@ -206,10 +215,85 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc); */ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size) { - return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); + u32 flags = TEE_SHM_MAPPED | TEE_SHM_REGISTER; + struct tee_shm *shm; + + shm = shm_alloc_helper(ctx, size, PAGE_SIZE, flags); + if (IS_ERR(shm)) + return shm; + + teedev_ctx_get(ctx); + return shm; } EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); +/** + * tee_shm_alloc_anon_kernel_buf() - Allocate shared memory for anonymous + * 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 two differences: + * 1. The memory might not be registered in secure world + * in case the driver supports passing memory not registered in advance. + * 2. The memory is not directly associated with the passed tee_context, + * rather the tee_device used by the context. + * + * This function should normally only be used internally in the TEE + * drivers. The memory must later only be freed using + * tee_shm_free_anon_kernel_buf() with a tee_contex with the same internal + * tee_device as when the memory was allocated. + * + * This allows allocating the shared memory using one context which is + * destroyed while the memory continues to live and finally freed using + * another context. + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_alloc_anon_kernel_buf(struct tee_context *ctx, + size_t size) +{ + struct tee_shm *shm; + + shm = shm_alloc_helper(ctx, size, sizeof(long) * 2, TEE_SHM_MAPPED); + if (IS_ERR(shm)) + return shm; + + shm->ctx = NULL; + return shm; +} +EXPORT_SYMBOL_GPL(tee_shm_alloc_anon_kernel_buf); + +/** + * tee_shm_free_anon_kernel_buf() - Free anonymous shared kernel memory + * @ctx: Borrowed context when freeing the shared memory + * @shm: Handle to shared memory to free + * + * This function must only be used to free a tee_shm allocated with + * tee_shm_alloc_anon_kernel_buf(). The passed @ctx has to have the same + * internal tee_device as was used by the tee_context passed when the + * memory was allocated. + */ +void tee_shm_free_anon_kernel_buf(struct tee_context *ctx, struct tee_shm *shm) +{ + struct tee_device *teedev = ctx->teedev; + + /* + * The anonymous kernel buffer isn't attached to any tee_context + * we're instead assigning the current tee_context temporarily. + * This is needed because an eventual call to unregister the shared + * memory might need a context. As long as this context uses the + * same tee_device as in the ctx in the call in + * tee_shm_alloc_anon_kernel_buf() above we are OK. + */ + shm->ctx = ctx; + teedev->pool->ops->free(teedev->pool, shm); + kfree(shm); + tee_device_put(teedev); +} +EXPORT_SYMBOL_GPL(tee_shm_free_anon_kernel_buf); + struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, size_t length, u32 flags) { diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 58b319766f8e..11a4e556bdf9 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -267,22 +267,11 @@ static inline void tee_shm_pool_free(struct tee_shm_pool *pool) */ void *tee_get_drvdata(struct tee_device *teedev); -/** - * 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 flags); +struct tee_shm *tee_shm_alloc_user_buf(struct tee_context *ctx, size_t size); struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size); +struct tee_shm *tee_shm_alloc_anon_kernel_buf(struct tee_context *ctx, + size_t size); +void tee_shm_free_anon_kernel_buf(struct tee_context *ctx, struct tee_shm *shm); /** * tee_shm_register() - Register shared memory buffer