From patchwork Wed Nov 29 12:48:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 119969 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3012899qgn; Wed, 29 Nov 2017 04:52:24 -0800 (PST) X-Google-Smtp-Source: AGs4zMZ70aA2zuhSKflAE8DUoeXaSnNUMwyuzqCRcvOJgPFyL94NFAH/XKd+CQUpp/lupCTq9C8C X-Received: by 10.84.202.194 with SMTP id q2mr2834325plh.19.1511959944570; Wed, 29 Nov 2017 04:52:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511959944; cv=none; d=google.com; s=arc-20160816; b=HyQNxauIa4KhRZ1DssFTZbZCQNaNi9mi5tOljsXQ0JN4rhgMAHQS4XyCmagOZc9yZb tvbovj07peJZWjGqjnDUGQIWDENLSHuxrCNzDaTpOtYeLh2Oe1sJ5UT1t87RGfqYYa5p +G8LiSNbfoK2GTaFP93KajP8nQQLX/JCZ494O9AYGFDaGMvf1sIkx7NEPogThENVWBmy LqR50ehdbBtIeQKCx6QhMUSkCOturvh7+Gq6+wL5tFp5qWNo9udV0iNSo6tbD6/OSDLM U4MHNvs92mTisOxvmDcyNit6aiVFVRcPKMASjz3698MUPWtbW2uSZpLSjPWk/L2AQC7P 92rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:arc-authentication-results; bh=iZi+k3+kTkx4OCmJLCfoAMxvHXdbv50DUKvYGWWi974=; b=OiYcgiVIg2z5A0WBKrePE+m5SYY2v84wFJigSfJ+1kHJ8K/c6QFVLXtCGitko2laFN q7WTrA3nIQaGpxF4YnJEXVfrZ1dYuzMgnnMJD/YJ4WWXMq4jR0iBH55I949ilflnGLVG JGt06HWkgZLbkSBdDmR0dPVK2jScbixdHxo+kqPvmAz4K+tqe/KDLch4QF4JC8c9gwwi fou8mbEbSQJv1JogAnRbuLC02dxqhhOike0lxcQlqf92KWH9itK5be70AootJ5Fd9xKE 3Fq4nfAC9rATbTmx1kfMYRVIv5aDGkh3tHcW2bGq4mufCZjA4odoQ+CnLu4SAlhWzb/i 4ZdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@epam.com header.s=selector1 header.b=cmcFZnVc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t14si1201677pgv.647.2017.11.29.04.52.24; Wed, 29 Nov 2017 04:52:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@epam.com header.s=selector1 header.b=cmcFZnVc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754828AbdK2MtF (ORCPT + 28 others); Wed, 29 Nov 2017 07:49:05 -0500 Received: from mail-he1eur01on0053.outbound.protection.outlook.com ([104.47.0.53]:28603 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932082AbdK2MtA (ORCPT ); Wed, 29 Nov 2017 07:49:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=iZi+k3+kTkx4OCmJLCfoAMxvHXdbv50DUKvYGWWi974=; b=cmcFZnVcHuJglIvBbHjKYZE4X6hrbOLL9DIuGryYne9EVXx3HcOsUwKjXCtkNSgDSNSQmW5EsquSYwDxQvUGjUW09B3dOMxOFcULNSmoKKD8HmcuUkbLY4jpToSYkahbzeiS5yr0PiWPba1iG6dm10BNaihvZXPOmkkYAPMC/9Y= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.56) by AM4PR03MB1763.eurprd03.prod.outlook.com (2603:10a6:200:10::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.282.5; Wed, 29 Nov 2017 12:48:54 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Wed, 29 Nov 2017 14:48:51 +0200 From: Volodymyr Babchuk To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, tee-dev@lists.linaro.org, Jens Wiklander Cc: volodymyr_babchuk@epam.com, Volodymyr Babchuk Subject: [RESEND PATCH v2 01/14] tee: flexible shared memory pool creation Date: Wed, 29 Nov 2017 14:48:25 +0200 Message-Id: <1511959718-5421-2-git-send-email-volodymyr_babchuk@epam.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com> References: <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com> MIME-Version: 1.0 X-Originating-IP: [85.223.209.56] X-ClientProxiedBy: AM0PR0102CA0042.eurprd01.prod.exchangelabs.com (2603:10a6:208::19) To AM4PR03MB1763.eurprd03.prod.outlook.com (2603:10a6:200:10::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e7f965a9-a40d-4f02-ae9d-08d537278cd8 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(5600026)(4604075)(4534020)(4602075)(7168020)(4627115)(201703031133081)(201702281549075)(2017052603199); SRVR:AM4PR03MB1763; X-Microsoft-Exchange-Diagnostics: 1; AM4PR03MB1763; 3:OLCm548Fp1BHWgoynEgIZ3vkjelI71v0phcdti3Qsq9H2SsYxcR6dSO6Xh+T+Mf0lfC+8vZDHqeOyjAL7ljJr9IXx+sUIK+UDBnF2GRQemtp74EfQXeMp4EnMsqG1KwIUFRnEnz7tVmIbV8gqoDanQsL5K+FLtf80dgDagZKnhkkqhL4vj0905zaRwJsa2QVszvNycDe3ED0kcj960HxdTL3hz8fxhcKP7l4B/lODd6c+vP9JYV7zrn3NFU6i9z5; 25:i++k9WUtgxJjmvUi6Lm6KAliHoJlzKRaqgGx5SSqnHTDesO7GOwNOioLkL+q21y0ooqkOWNhzqQ5lSt7Z1m3+t+ZORO+0XzivU4GzuQGkOe/MNt68b7OdVkIgCeWcv+qtz/J49iohJeTLdY475/i9eRvT6HKw8ERmcI+FyZEnT7TzTRc8QpZOq7QDPtHN+xoFrD2opXzrZnXeatPExKb4GogHkX1mCqjIv0E+Uq9KtlrcJFquae9PSMClQgftr374UseXzMNO7ZmzSzH/5egOC2M194VSgKOjuSsmb/EfiJpfCOUBKmcEqCjG6nVlrMW7xj9ig6LpK8vP2GY20qlxQ==; 31:LzVZUwp6BIiTiy7A3ZqvHyouyjK3TOODg7E5PcMB9GqCTIncyg/x57hy+6EstNzdw2uUdSd2ACQFzJ3yRA+VtezepfAucPvfMXd5qqFK1wkygOCMxrXCoXdzBV34rn1UtrYz9rxJqMoPeZTwEYk4hbkKElp5W0OLTRMCHlxhCBy9en1MGckin0g8M/K+Z0NKfks3EaIj510TSy1jfba8Yhj2BPUpz2MYwkYlMdGhjNc= X-MS-TrafficTypeDiagnostic: AM4PR03MB1763: X-Microsoft-Exchange-Diagnostics: 1; AM4PR03MB1763; 20:fdIBkxS0L+KI0StPgRHMns0MrAps14hmsLXd9/snw4VUncbmqMYS9UCTrhHHo7sS6ORqmanfW0q/Jq8howrWQgFyAE0LyN1C9XsApkrPh+3abRQdwv4AKG+f5zYymRHRrtGjj8PpxBJA9nly0H9tgMXKDzHwUjg0T9lIUWb/cFxfQ4y/bAV+kuZwrhq6hc36snE/ATis8VFTXOzsscyDq64g0ska7QDFIrRzkBis1mjbG2fn6sytwz3k5/HR6yBJQuQRkYsUntW5cY+KZGU4gZX3EHq4pWs0xgUTRg3p3fEVemjOqdTvaFxYzUswk2MPYuc5NpmCK1TyO9jOD2kpl/OtTjRPzssDlJtycJ0IQBQb7x0vRHoR15oj5SG2xFW0amGteY6aex/fLnrwk33uJOkugQOrJSZKAGG/HuPwHZla+A2KY//PsZJMTaTdkuQu32NBgVv8bL5xWn4akAcxs7qeQbuc2EUz+TnWkRKWp3LlFK9IF5BUmUAWcnGRpS2h; 4:aWgJWg3k/nk79jsp2LcnTnd4aDpLjdlpBXOCYbFneUrK/Sg+P5dsFnTdNg0H4mT65VOE3vHHLtc8Cr0EpnQm4AkpRKdrRDzgY+UOIRb5zyRzu6fsZpxea0FQ36w2wi2zYlmE/DyjNLwiBIWZD11199dMRbGKsSLlPGLhovFJzQXYlV334jYdyMJw+1X3pV62r8heF0XVlVl2ZiLhfrrL1j4gkXClfyroThmNFYOnPxvNUTdZmqsE2ZIScbmx70r8FBh+SyEieM2/icYiw0cZwg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(2401047)(8121501046)(5005006)(3231022)(3002001)(10201501046)(93006095)(93001095)(6041248)(20161123560025)(20161123564025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123558100)(6072148)(201708071742011); SRVR:AM4PR03MB1763; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:AM4PR03MB1763; X-Forefront-PRVS: 05066DEDBB X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(376002)(366004)(346002)(199003)(189002)(50986999)(68736007)(76176999)(97736004)(101416001)(6916009)(2950100002)(2906002)(80792005)(6666003)(6116002)(4326008)(53936002)(3846002)(316002)(81166006)(106356001)(8936002)(33646002)(81156014)(72206003)(16586007)(5660300001)(36756003)(42186006)(305945005)(478600001)(50226002)(48376002)(51416003)(7736002)(8676002)(105586002)(50466002)(66066001)(55236003)(47776003)(86362001)(52116002)(189998001)(122856001)(39060400002)(217873001); DIR:OUT; SFP:1101; SCL:1; SRVR:AM4PR03MB1763; H:EPUAKYIW2556.kyiv.epam.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: epam.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: 1; AM4PR03MB1763; 23:LvnNX3bxHGz4LWOfuhmEZRzWk/HdcfMP1ePR1rgF3JPqQ8KDlmoK+l21aoHExBxowBGuquJF08qcdAKexU4c5AVT4h52yrmG2S5gJn1tIWoUNm/293BWiG/FvtIjRMCsdu5a6CXkuf43r5hhwtvSvYGVXGEa+Vg0DX4KXWvn3U39a4YxcV94H7c+Z3483ZJJ6Ob5edhAkv836w5PY+v1+D8Pg9xw60jUG31eudbVjwu/7YAKR5L9KOZwgs7Kv6xjrsz8fE2buWa34XFtPvfT02Q7XI1BSC3F5bDWVy9CJzy8+F3ZAlqJiFTAP40I8+sWNqoOsx6OmPmBXQLD1BKNbEhIgumHx/U+xk0PpQDRhJ6cVskITU6ftTWCnG0ZANr13j0y6pgbmVS3LElLvhxE+vtHCSv/IkNVUHuj5Ae8z1ohgs5zWNazUJUnLhAvhTqeUFOV6Lw34gp/FAebnoXUQfvtBDXKF2c3XqHKJbTb18j++DNFzhJFXhsrpJTGQd2wZ/Y0jVM3R5BpOctqGQ2E1Qk4ibQOH8KItZx7XIAlXOUM/huFqP3nWQe171ChwYRheFcP4hbcvhAoUGYbzc6envcmkBSPVpiASpJLFUUzFs++NoKKvuNNVzJbWSXgv6cWq5NzpJH8qPmV9muf0dTJ+05p0U9yzlPHP4HRlbzBT0NRzSOje3g1e0umE8cq9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65KtWcoO7zMys/5S/Ypg+8bwxauCwVqj+s5WU1yttYD1jIHd5QsWDMSqSgiH0MLyFLWEqsg=; 7:k8V0uYGCTkHYZR/ZyrUAhi+J8d4H4Kk6hE3mO1w1GGT8geKKfA8YOiJArmCS5/pP7zogrOns13Itk8oBY2NZDMduYAV+wUU0DFlBUwjNrrN7dgoR2BrhP+wduENDr70oNlow43c1GRf8o3tvazSEU5OMbMvLEr7ymnFTXiwSfmGX1ilfG+eEv2+eTMddvimlXaPeH8AMJchufI5N5YeiAvZUU2A2dN1NnjGDVV7SgiSfMeUYKSsvEpvF27nhQexa SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2017 12:48:54.5305 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e7f965a9-a40d-4f02-ae9d-08d537278cd8 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR03MB1763 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jens Wiklander Makes creation of shm pools more flexible by adding new more primitive functions to allocate a shm pool. This makes it easier to add driver specific shm pool management. Signed-off-by: Jens Wiklander Signed-off-by: Volodymyr Babchuk --- drivers/tee/tee_private.h | 57 +--------------- drivers/tee/tee_shm.c | 8 +-- drivers/tee/tee_shm_pool.c | 165 ++++++++++++++++++++++++++++----------------- include/linux/tee_drv.h | 91 +++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 122 deletions(-) -- 2.7.4 diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 21cb6be..2bc2b5a 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -21,68 +21,15 @@ #include #include -struct tee_device; - -/** - * struct tee_shm - shared memory object - * @teedev: device used to allocate the object - * @ctx: context using the object, if NULL the context is gone - * @link link element - * @paddr: physical address of the shared memory - * @kaddr: virtual address of the shared memory - * @size: size of shared memory - * @dmabuf: dmabuf used to for exporting to user space - * @flags: defined by TEE_SHM_* in tee_drv.h - * @id: unique id of a shared memory object on this device - */ -struct tee_shm { - struct tee_device *teedev; - struct tee_context *ctx; - struct list_head link; - phys_addr_t paddr; - void *kaddr; - size_t size; - struct dma_buf *dmabuf; - u32 flags; - int id; -}; - -struct tee_shm_pool_mgr; - -/** - * struct tee_shm_pool_mgr_ops - shared memory pool manager operations - * @alloc: called when allocating shared memory - * @free: called when freeing shared memory - */ -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); -}; - -/** - * struct tee_shm_pool_mgr - shared memory manager - * @ops: operations - * @private_data: private data for the shared memory manager - */ -struct tee_shm_pool_mgr { - const struct tee_shm_pool_mgr_ops *ops; - void *private_data; -}; - /** * 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 - * @destroy: called when destroying the pool - * @private_data: private data for the pool */ struct tee_shm_pool { - struct tee_shm_pool_mgr private_mgr; - struct tee_shm_pool_mgr dma_buf_mgr; - void (*destroy)(struct tee_shm_pool *pool); - void *private_data; + struct tee_shm_pool_mgr *private_mgr; + struct tee_shm_pool_mgr *dma_buf_mgr; }; #define TEE_DEVICE_FLAG_REGISTERED 0x1 diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 4bc7956..fdda89e 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -32,9 +32,9 @@ static void tee_shm_release(struct tee_shm *shm) mutex_unlock(&teedev->mutex); if (shm->flags & TEE_SHM_DMA_BUF) - poolm = &teedev->pool->dma_buf_mgr; + poolm = teedev->pool->dma_buf_mgr; else - poolm = &teedev->pool->private_mgr; + poolm = teedev->pool->private_mgr; poolm->ops->free(poolm, shm); kfree(shm); @@ -139,9 +139,9 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) shm->teedev = teedev; shm->ctx = ctx; if (flags & TEE_SHM_DMA_BUF) - poolm = &teedev->pool->dma_buf_mgr; + poolm = teedev->pool->dma_buf_mgr; else - poolm = &teedev->pool->private_mgr; + poolm = teedev->pool->private_mgr; rc = poolm->ops->alloc(poolm, shm, size); if (rc) { diff --git a/drivers/tee/tee_shm_pool.c b/drivers/tee/tee_shm_pool.c index fb4f852..e6d4b9e 100644 --- a/drivers/tee/tee_shm_pool.c +++ b/drivers/tee/tee_shm_pool.c @@ -44,49 +44,18 @@ static void pool_op_gen_free(struct tee_shm_pool_mgr *poolm, shm->kaddr = NULL; } +static void pool_op_gen_destroy_poolmgr(struct tee_shm_pool_mgr *poolm) +{ + gen_pool_destroy(poolm->private_data); + kfree(poolm); +} + static const struct tee_shm_pool_mgr_ops pool_ops_generic = { .alloc = pool_op_gen_alloc, .free = pool_op_gen_free, + .destroy_poolmgr = pool_op_gen_destroy_poolmgr, }; -static void pool_res_mem_destroy(struct tee_shm_pool *pool) -{ - gen_pool_destroy(pool->private_mgr.private_data); - gen_pool_destroy(pool->dma_buf_mgr.private_data); -} - -static int pool_res_mem_mgr_init(struct tee_shm_pool_mgr *mgr, - struct tee_shm_pool_mem_info *info, - int min_alloc_order) -{ - size_t page_mask = PAGE_SIZE - 1; - struct gen_pool *genpool = NULL; - int rc; - - /* - * Start and end must be page aligned - */ - if ((info->vaddr & page_mask) || (info->paddr & page_mask) || - (info->size & page_mask)) - return -EINVAL; - - genpool = gen_pool_create(min_alloc_order, -1); - if (!genpool) - return -ENOMEM; - - gen_pool_set_algo(genpool, gen_pool_best_fit, NULL); - rc = gen_pool_add_virt(genpool, info->vaddr, info->paddr, info->size, - -1); - if (rc) { - gen_pool_destroy(genpool); - return rc; - } - - mgr->private_data = genpool; - mgr->ops = &pool_ops_generic; - return 0; -} - /** * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved * memory range @@ -104,42 +73,109 @@ 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 *pool = NULL; - int ret; - - pool = kzalloc(sizeof(*pool), GFP_KERNEL); - if (!pool) { - ret = -ENOMEM; - goto err; - } + struct tee_shm_pool_mgr *priv_mgr; + struct tee_shm_pool_mgr *dmabuf_mgr; + void *rc; /* * Create the pool for driver private shared memory */ - ret = pool_res_mem_mgr_init(&pool->private_mgr, priv_info, - 3 /* 8 byte aligned */); - if (ret) - goto err; + 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 */ - ret = pool_res_mem_mgr_init(&pool->dma_buf_mgr, dmabuf_info, - PAGE_SHIFT); - if (ret) + 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, + int min_alloc_order) +{ + const size_t page_mask = PAGE_SIZE - 1; + struct tee_shm_pool_mgr *mgr; + 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) + return ERR_PTR(-ENOMEM); + + mgr->private_data = gen_pool_create(min_alloc_order, -1); + if (!mgr->private_data) { + rc = -ENOMEM; goto err; + } - pool->destroy = pool_res_mem_destroy; - return pool; + gen_pool_set_algo(mgr->private_data, gen_pool_best_fit, NULL); + rc = gen_pool_add_virt(mgr->private_data, vaddr, paddr, size, -1); + if (rc) { + gen_pool_destroy(mgr->private_data); + goto err; + } + + mgr->ops = &pool_ops_generic; + + return mgr; err: - if (ret == -ENOMEM) - pr_err("%s: can't allocate memory for res_mem shared memory pool\n", __func__); - if (pool && pool->private_mgr.private_data) - gen_pool_destroy(pool->private_mgr.private_data); - kfree(pool); - return ERR_PTR(ret); + kfree(mgr); + + return ERR_PTR(rc); } -EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem); +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 @@ -150,7 +186,10 @@ EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem); */ void tee_shm_pool_free(struct tee_shm_pool *pool) { - pool->destroy(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); diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index cb889af..e9be4a4 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -150,6 +150,97 @@ int tee_device_register(struct tee_device *teedev); void tee_device_unregister(struct tee_device *teedev); /** + * struct tee_shm - shared memory object + * @teedev: device used to allocate the object + * @ctx: context using the object, if NULL the context is gone + * @link link element + * @paddr: physical address of the shared memory + * @kaddr: virtual address of the shared memory + * @size: size of shared memory + * @offset: offset of buffer in user space + * @pages: locked pages from userspace + * @num_pages: number of locked pages + * @dmabuf: dmabuf used to for exporting to user space + * @flags: defined by TEE_SHM_* in tee_drv.h + * @id: unique id of a shared memory object on this device + * + * This pool is only supposed to be accessed directly from the TEE + * subsystem and from drivers that implements their own shm pool manager. + */ +struct tee_shm { + struct tee_device *teedev; + struct tee_context *ctx; + struct list_head link; + phys_addr_t paddr; + void *kaddr; + size_t size; + unsigned int offset; + struct page **pages; + size_t num_pages; + struct dma_buf *dmabuf; + u32 flags; + int id; +}; + +/** + * struct tee_shm_pool_mgr - shared memory manager + * @ops: operations + * @private_data: private data for the shared memory manager + */ +struct tee_shm_pool_mgr { + const struct tee_shm_pool_mgr_ops *ops; + void *private_data; +}; + +/** + * struct tee_shm_pool_mgr_ops - shared memory pool manager operations + * @alloc: called when allocating shared memory + * @free: called when freeing shared memory + * @destroy_poolmgr: called when destroying the pool manager + */ +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); +}; + +/** + * 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 + * @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 + */ +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 From patchwork Wed Nov 29 12:48:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 119970 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3013022qgn; Wed, 29 Nov 2017 04:52:31 -0800 (PST) X-Google-Smtp-Source: AGs4zMYz2FrgD+q5JNpRVcAlvAac9VSGfTEIDOr5Dd7ED2WOCceO6N3VtE1/8IXtxJLNgRH4dED2 X-Received: by 10.84.170.132 with SMTP id j4mr2853468plb.316.1511959951125; Wed, 29 Nov 2017 04:52:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511959951; cv=none; d=google.com; s=arc-20160816; b=OD8rmbagX6In/y+eii/QkrbJkitj2VIru59bTm/voOXbEfRrbwnSLosw4it7l94s9x wlvv2DHbVWAJQjncSQVSbedhoVbNasBqCNGaRBWuYOVDAGXx36Z+pIoQ9xpiGfaNwGKs Yf9ciy+j8QELWvOPo7Ht/gn1CVrR0ES0PrIacREd5A8YqvsNNrtWIaJ55Ulji9DT+lk6 E90sOVTPtorfo6hDYV/xFBfWhIXpXWm5HyoenRp0GlayxFnU0aft71h9S/E+TCD+wQrV anEmZtytjP8yeH7FlcB2q8uYqD1z1Mh+3kAp/W3gs/8vTJPLCtYhaA3Agw0nWW8VOhT6 EZSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:arc-authentication-results; bh=HG6Npjpz8VGKckh50V95sX5qSc0nk7FH2d8YRLD8g4U=; b=hhOm+x5rcf3kfjGpMENXdIAjTjSTLC/BJiWnm2vWGfcRR8ZLlAegV/GmIxRReFWqoB eXw5VdFKzuLcytCiPqUdEeagwer3iosYP9a3FN8U5d79xrGyYXnCyCeCMeYgVcmPu59O dikBhgYPBPkzRqGjpjA8MhfALljdevIqXhfO0tMioVLImMBx7L2Jy6enXSdTyggw2I+w zpTgOVOK5fxhCmpkhjeITpIPGahqt9m6nGCfGEmDHkbMtTZrhEAHOs8q6H13c4XLOz7h B+nH3vCsn406LVwG0WiER5c/aiemkyGOH0RttvKPg67Z3VTGmCN6EA4x9v1TQyoUTpqQ kpXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@epam.com header.s=selector1 header.b=aXUz5hvB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r16si1321409pfk.363.2017.11.29.04.52.30; Wed, 29 Nov 2017 04:52:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@epam.com header.s=selector1 header.b=aXUz5hvB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753249AbdK2Mw2 (ORCPT + 28 others); Wed, 29 Nov 2017 07:52:28 -0500 Received: from mail-he1eur01on0068.outbound.protection.outlook.com ([104.47.0.68]:36480 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754811AbdK2MtF (ORCPT ); Wed, 29 Nov 2017 07:49:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=HG6Npjpz8VGKckh50V95sX5qSc0nk7FH2d8YRLD8g4U=; b=aXUz5hvBuI38p0XFciicukf/IM+nQkiPILmDKg1YeVScTq8Pv/h+mCn7XkqFoD166BpiOdooVeI0CdXhX29AF/fv2g6A5cp4OjVN58dsmLEj/uoedXnwegvobyTdFqYUyK1+r/Ae+wvOYlaK/Vl6rg+zr7owtGNqGm2Uw5+6mOo= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.56) by AM4PR03MB1763.eurprd03.prod.outlook.com (2603:10a6:200:10::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.282.5; Wed, 29 Nov 2017 12:49:00 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Wed, 29 Nov 2017 14:48:56 +0200 From: Volodymyr Babchuk To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, tee-dev@lists.linaro.org, Jens Wiklander Cc: volodymyr_babchuk@epam.com, Volodymyr Babchuk Subject: [RESEND PATCH v2 02/14] tee: add register user memory Date: Wed, 29 Nov 2017 14:48:26 +0200 Message-Id: <1511959718-5421-3-git-send-email-volodymyr_babchuk@epam.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com> References: <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com> MIME-Version: 1.0 X-Originating-IP: [85.223.209.56] X-ClientProxiedBy: HE1PR0402CA0031.eurprd04.prod.outlook.com (2603:10a6:7:7c::20) To AM4PR03MB1763.eurprd03.prod.outlook.com (2603:10a6:200:10::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f7ed1941-8daa-4c91-8de9-08d5372790b8 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(5600026)(4604075)(4534020)(4602075)(7168020)(4627115)(201703031133081)(201702281549075)(2017052603199); SRVR:AM4PR03MB1763; X-Microsoft-Exchange-Diagnostics: 1; AM4PR03MB1763; 3:3YAhU/P5MWgHyfMNekz4JQi/DqERwSU48SQVOrKbfMGk8K5+quYLezkguPRbNIv3mHOMUtRTQYlMy1Ye6yF0Ciw58Z845Bt9L3gaV8/G0eZRcpCu+LI8nDkxBK6oBmkL6widEjt947myhQgvh/BjiZy14Y5mjFOA8rIhn57Ax1w5ZJxs0ocEMuS8DDjs4gixS6uWEsqaUnlb+G1643kV0RzkZM0wUEUJAbwow25RdZMSXnyHKqv9Bl9escSn/qgz; 25:LxM2rvMZ6Qg+fGFNAE/XxNRGi/8+m8zr9jKNGiSCubqcquJVrr1piVmXV1HZtXVeXWt7eA27QdG+Lxgq5r5baTNM2KCpGvHhkl3Y/rJFAAagcY1N+U8IduZOdf7ZyjbGKx5xrSLG/O6gVIhbXELlkIci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goui37uhglGVztCHS1+06u9UA6iXmag7lZcYL7cJ7tUcggasyGZgrzKFPDH6WusovSkJ3A9hqoUuylZHAoQ5T/hR1HGxNn60D8nidRz1gwMATnp+VoKBI0NymEi0M4POo/2RskWlhAyfffg/GyTORBx2/OuJZMRoJOzacSJg/VW2XwSuhyTxbHiOM6k6nKzeIinYHq82fCG0r6T7uyJJ40mLqVwOg0w== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(2401047)(8121501046)(5005006)(3231022)(3002001)(10201501046)(93006095)(93001095)(6041248)(20161123560025)(20161123564025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123558100)(6072148)(201708071742011); SRVR:AM4PR03MB1763; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:AM4PR03MB1763; X-Forefront-PRVS: 05066DEDBB X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(376002)(366004)(346002)(199003)(189002)(50986999)(68736007)(76176999)(97736004)(101416001)(6916009)(2950100002)(2906002)(80792005)(6666003)(6116002)(4326008)(53936002)(3846002)(316002)(81166006)(106356001)(8936002)(33646002)(81156014)(72206003)(16586007)(5660300001)(36756003)(42186006)(305945005)(478600001)(50226002)(48376002)(51416003)(7736002)(8676002)(105586002)(50466002)(66066001)(55236003)(47776003)(86362001)(52116002)(189998001)(122856001)(39060400002)(217873001); DIR:OUT; SFP:1101; SCL:1; SRVR:AM4PR03MB1763; H:EPUAKYIW2556.kyiv.epam.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: epam.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: 1; AM4PR03MB1763; 23:mAuJkhTDmrzZLsWSw8Ap3sgUBTwrh4QXndk6XqnhEVoTnqE4eNo1g/2oGHRT1h88f212k9tIRytiqf7ulcskf/HT97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-Diagnostics: 1; AM4PR03MB1763; 6:H/npvuTpzMLG7jIIy3QQH6Oh+YueS4vztx+0XlwkwETkC6HZKLnAO5ZtReGISNWg0JYgwnZWDeJQg5UNgY/sU7QkBSNsxmKVlKVLg/vloLs/EG3eSsy3tknxPQd/7pWz786Ua7pJV83YMKbMbnMypBrBCsGhuoitG1WtWKvc+OJee2yw/E+HHhCvIQMubKRY3MtnOasMv/FzAZIyXIZUygOvSLBVttuQE83ms1QiLPamgFvU94LpR/kLxeZV5Eq8vKeyjG7OZc8CMg2s5I9xOvSSG/y6p2TWX5E9sYmsy78bfrLZziRF7Vry/zh8/VxzJdGHKelxIJH/nfYYefJmiABZpFqlmhjc3qApnvwP1tE=; 5:1IwcRd8VpkmtarsFa1uKl2xQbCHPVVm8q/1YbmwZvFLxzadetyPuwh5r0mpFa9qLqbkXLyNUpjrWyaNVZ9/xbzYEd13Va/iwfOxyYLteXbiiw/BsKqeJcGGrvhqK4AYyTD3BZLYqFSbtH0TRQb7ug1w50YxoE45VFtzxLNSwxZI=; 24:hHTyU2009Um2cOO7kKa+yv66xuOQ/0qxw3GjRlKczqAe2ic1oaVjz37NKmdoo5/ZA1IEfj74qMXxQdj6wqevksTTCb/2Kvsxi+AxwEpoWbc=; 7:82JaVCnOHF6eXw51x3HFtferOCv39WV8pLSRGa0VYsUrSCddCaxapjGdAPH78gV4Sfdt0ZEKilWRbdgBj7nJuBxhvKu/Un/p8uHF+8cCZbP8shk2MuWQckNZpgMAD3qdUeL24dZ8TjGlXHg02gQKnCEqZO7mKz+dJ1VHBMzE0xPM9jJlIz9cvwtSi3tb9REVFRD6OHu9xkC82G5nRCCTaB7k+sHaco14rdfsARU/JcyYHF2MtpgXyuZ3WRzVaPRf SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2017 12:49:00.6559 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f7ed1941-8daa-4c91-8de9-08d5372790b8 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR03MB1763 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jens Wiklander Added new ioctl to allow users register own buffers as a shared memory. Signed-off-by: Jens Wiklander Signed-off-by: Volodymyr Babchuk --- drivers/tee/tee_core.c | 41 +++++++++- drivers/tee/tee_shm.c | 205 +++++++++++++++++++++++++++++++++++++++++------ include/linux/tee_drv.h | 47 ++++++++++- include/uapi/linux/tee.h | 30 +++++++ 4 files changed, 293 insertions(+), 30 deletions(-) -- 2.7.4 diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 58a5009..295910f 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -114,8 +114,6 @@ static int tee_ioctl_shm_alloc(struct tee_context *ctx, if (data.flags) return -EINVAL; - data.id = -1; - shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); if (IS_ERR(shm)) return PTR_ERR(shm); @@ -138,6 +136,43 @@ static int tee_ioctl_shm_alloc(struct tee_context *ctx, return ret; } +static int +tee_ioctl_shm_register(struct tee_context *ctx, + struct tee_ioctl_shm_register_data __user *udata) +{ + long ret; + struct tee_ioctl_shm_register_data data; + struct tee_shm *shm; + + if (copy_from_user(&data, udata, sizeof(data))) + return -EFAULT; + + /* Currently no input flags are supported */ + if (data.flags) + return -EINVAL; + + shm = tee_shm_register(ctx, data.addr, data.length, + TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED); + if (IS_ERR(shm)) + return PTR_ERR(shm); + + data.id = shm->id; + data.flags = shm->flags; + data.length = shm->size; + + if (copy_to_user(udata, &data, sizeof(data))) + ret = -EFAULT; + else + ret = tee_shm_get_fd(shm); + /* + * When user space closes the file descriptor the shared memory + * should be freed or if tee_shm_get_fd() failed then it will + * be freed immediately. + */ + tee_shm_put(shm); + return ret; +} + static int params_from_user(struct tee_context *ctx, struct tee_param *params, size_t num_params, struct tee_ioctl_param __user *uparams) @@ -586,6 +621,8 @@ static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return tee_ioctl_version(ctx, uarg); case TEE_IOC_SHM_ALLOC: return tee_ioctl_shm_alloc(ctx, uarg); + case TEE_IOC_SHM_REGISTER: + return tee_ioctl_shm_register(ctx, uarg); case TEE_IOC_OPEN_SESSION: return tee_ioctl_open_session(ctx, uarg); case TEE_IOC_INVOKE: diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index fdda89e..177e341 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -23,7 +23,6 @@ static void tee_shm_release(struct tee_shm *shm) { struct tee_device *teedev = shm->teedev; - struct tee_shm_pool_mgr *poolm; mutex_lock(&teedev->mutex); idr_remove(&teedev->idr, shm->id); @@ -31,12 +30,29 @@ static void tee_shm_release(struct tee_shm *shm) list_del(&shm->link); mutex_unlock(&teedev->mutex); - if (shm->flags & TEE_SHM_DMA_BUF) - poolm = teedev->pool->dma_buf_mgr; - else - poolm = teedev->pool->private_mgr; + 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); + } else if (shm->flags & TEE_SHM_REGISTER) { + size_t n; + int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm); + + if (rc) + dev_err(teedev->dev.parent, + "unregister shm %p failed: %d", shm, rc); + + for (n = 0; n < shm->num_pages; n++) + put_page(shm->pages[n]); + + kfree(shm->pages); + } - poolm->ops->free(poolm, shm); kfree(shm); tee_device_put(teedev); @@ -76,6 +92,10 @@ static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) struct tee_shm *shm = dmabuf->priv; size_t size = vma->vm_end - vma->vm_start; + /* Refuse sharing shared memory provided by application */ + if (shm->flags & TEE_SHM_REGISTER) + return -EINVAL; + return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT, size, vma->vm_page_prot); } @@ -89,26 +109,20 @@ static const struct dma_buf_ops tee_shm_dma_buf_ops = { .mmap = tee_shm_op_mmap, }; -/** - * 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_* in . 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. - */ -struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) +struct tee_shm *__tee_shm_alloc(struct tee_context *ctx, + struct tee_device *teedev, + size_t size, u32 flags) { - struct tee_device *teedev = ctx->teedev; struct tee_shm_pool_mgr *poolm = NULL; struct tee_shm *shm; void *ret; int rc; + if (ctx && ctx->teedev != teedev) { + dev_err(teedev->dev.parent, "ctx and teedev mismatch\n"); + return ERR_PTR(-EINVAL); + } + if (!(flags & TEE_SHM_MAPPED)) { dev_err(teedev->dev.parent, "only mapped allocations supported\n"); @@ -135,7 +149,7 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) goto err_dev_put; } - shm->flags = flags; + shm->flags = flags | TEE_SHM_POOL; shm->teedev = teedev; shm->ctx = ctx; if (flags & TEE_SHM_DMA_BUF) @@ -171,9 +185,12 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) goto err_rem; } } - mutex_lock(&teedev->mutex); - list_add_tail(&shm->link, &ctx->list_shm); - mutex_unlock(&teedev->mutex); + + if (ctx) { + mutex_lock(&teedev->mutex); + list_add_tail(&shm->link, &ctx->list_shm); + mutex_unlock(&teedev->mutex); + } return shm; err_rem: @@ -188,8 +205,139 @@ struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) tee_device_put(teedev); return ret; } +/** + * 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_* in . 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. + */ +struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) +{ + return __tee_shm_alloc(ctx, ctx->teedev, size, flags); +} EXPORT_SYMBOL_GPL(tee_shm_alloc); +struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size) +{ + return __tee_shm_alloc(NULL, teedev, size, TEE_SHM_MAPPED); +} +EXPORT_SYMBOL_GPL(tee_shm_priv_alloc); + +struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, + size_t length, u32 flags) +{ + struct tee_device *teedev = ctx->teedev; + const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED; + struct tee_shm *shm; + void *ret; + int rc; + int num_pages; + unsigned long start; + + if (flags != req_flags) + return ERR_PTR(-ENOTSUPP); + + if (!tee_device_get(teedev)) + return ERR_PTR(-EINVAL); + + if (!teedev->desc->ops->shm_register || + !teedev->desc->ops->shm_unregister) { + tee_device_put(teedev); + return ERR_PTR(-ENOTSUPP); + } + + shm = kzalloc(sizeof(*shm), GFP_KERNEL); + if (!shm) { + ret = ERR_PTR(-ENOMEM); + goto err; + } + + shm->flags = flags | TEE_SHM_REGISTER; + shm->teedev = teedev; + shm->ctx = ctx; + shm->id = -1; + start = rounddown(addr, PAGE_SIZE); + shm->offset = addr - start; + shm->size = length; + num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE; + shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL); + if (!shm->pages) { + ret = ERR_PTR(-ENOMEM); + goto err; + } + + rc = get_user_pages_fast(start, num_pages, 1, shm->pages); + if (rc > 0) + shm->num_pages = rc; + if (rc != num_pages) { + if (rc > 0) + rc = -ENOMEM; + ret = ERR_PTR(rc); + goto err; + } + + 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; + } + + rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages, + shm->num_pages); + if (rc) { + ret = ERR_PTR(rc); + goto err; + } + + if (flags & TEE_SHM_DMA_BUF) { + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + + 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); + teedev->desc->ops->shm_unregister(ctx, shm); + goto err; + } + } + + mutex_lock(&teedev->mutex); + list_add_tail(&shm->link, &ctx->list_shm); + mutex_unlock(&teedev->mutex); + + return shm; +err: + if (shm) { + size_t n; + + if (shm->id >= 0) { + mutex_lock(&teedev->mutex); + idr_remove(&teedev->idr, shm->id); + mutex_unlock(&teedev->mutex); + } + for (n = 0; n < shm->num_pages; n++) + put_page(shm->pages[n]); + kfree(shm->pages); + } + kfree(shm); + tee_device_put(teedev); + return ret; +} +EXPORT_SYMBOL_GPL(tee_shm_register); + /** * tee_shm_get_fd() - Increase reference count and return file descriptor * @shm: Shared memory handle @@ -197,10 +345,9 @@ EXPORT_SYMBOL_GPL(tee_shm_alloc); */ int tee_shm_get_fd(struct tee_shm *shm) { - u32 req_flags = TEE_SHM_MAPPED | TEE_SHM_DMA_BUF; int fd; - if ((shm->flags & req_flags) != req_flags) + if (!(shm->flags & TEE_SHM_DMA_BUF)) return -EINVAL; fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC); @@ -238,6 +385,8 @@ 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)) + return -EINVAL; /* Check that we're in the range of the shm */ if ((char *)va < (char *)shm->kaddr) return -EINVAL; @@ -258,6 +407,8 @@ 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)) + return -EINVAL; /* Check that we're in the range of the shm */ if (pa < shm->paddr) return -EINVAL; @@ -284,6 +435,8 @@ EXPORT_SYMBOL_GPL(tee_shm_pa2va); */ void *tee_shm_get_va(struct tee_shm *shm, size_t offs) { + if (!(shm->flags & TEE_SHM_MAPPED)) + return ERR_PTR(-EINVAL); if (offs >= shm->size) return ERR_PTR(-EINVAL); return (char *)shm->kaddr + offs; diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index e9be4a4..70b9c73 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -25,8 +25,12 @@ * specific TEE driver. */ -#define TEE_SHM_MAPPED 0x1 /* Memory mapped by the kernel */ -#define TEE_SHM_DMA_BUF 0x2 /* Memory with dma-buf handle */ +#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 */ struct device; struct tee_device; @@ -76,6 +80,8 @@ struct tee_param { * @cancel_req: request cancel of an ongoing invoke or open * @supp_revc: called for supplicant to get a command * @supp_send: called for supplicant to send a response + * @shm_register: register shared memory buffer in TEE + * @shm_unregister: unregister shared memory buffer in TEE */ struct tee_driver_ops { void (*get_version)(struct tee_device *teedev, @@ -94,6 +100,9 @@ struct tee_driver_ops { struct tee_param *param); int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params, struct tee_param *param); + int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, + struct page **pages, size_t num_pages); + int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm); }; /** @@ -302,6 +311,30 @@ void *tee_get_drvdata(struct tee_device *teedev); struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags); /** + * tee_shm_priv_alloc() - Allocate shared memory privately + * @dev: Device that allocates the shared memory + * @size: Requested size of shared memory + * + * Allocates shared memory buffer that is not associated with any client + * context. Such buffers are owned by TEE driver and used for internal calls. + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size); + +/** + * 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 addr, + size_t length, u32 flags); + +/** * tee_shm_free() - Free shared memory * @shm: Handle to shared memory to free */ @@ -366,4 +399,14 @@ int tee_shm_get_id(struct tee_shm *shm); */ struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id); +/** + * tee_shm_is_registered() - Check if shared memory object in registered in TEE + * @shm: Shared memory handle + * @returns true if object is registered in TEE + */ +static inline bool tee_shm_is_registered(struct tee_shm *shm) +{ + return shm && (shm->flags & TEE_SHM_REGISTER); +} + #endif /*__TEE_DRV_H*/ diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h index 688782e..d41a07a 100644 --- a/include/uapi/linux/tee.h +++ b/include/uapi/linux/tee.h @@ -50,6 +50,7 @@ #define TEE_GEN_CAP_GP (1 << 0)/* GlobalPlatform compliant TEE */ #define TEE_GEN_CAP_PRIVILEGED (1 << 1)/* Privileged device (for supplicant) */ +#define TEE_GEN_CAP_REG_MEM (1 << 2)/* Supports registering shared memory */ /* * TEE Implementation ID @@ -332,6 +333,35 @@ struct tee_iocl_supp_send_arg { #define TEE_IOC_SUPPL_SEND _IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 7, \ struct tee_ioctl_buf_data) +/** + * struct tee_ioctl_shm_register_data - Shared memory register argument + * @addr: [in] Start address of shared memory to register + * @length: [in/out] Length of shared memory to register + * @flags: [in/out] Flags to/from registration. + * @id: [out] Identifier of the shared memory + * + * The flags field should currently be zero as input. Updated by the call + * with actual flags as defined by TEE_IOCTL_SHM_* above. + * This structure is used as argument for TEE_IOC_SHM_REGISTER below. + */ +struct tee_ioctl_shm_register_data { + __u64 addr; + __u64 length; + __u32 flags; + __s32 id; +}; + +/** + * TEE_IOC_SHM_REGISTER - Register shared memory argument + * + * Registers shared memory between the user space process and secure OS. + * + * Returns a file descriptor on success or < 0 on failure + * + * The shared memory is unregisterred when the descriptor is closed. + */ +#define TEE_IOC_SHM_REGISTER _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 9, \ + struct tee_ioctl_shm_register_data) /* * Five syscalls are used when communicating with the TEE driver. * open(): opens the device associated with the driver