From patchwork Fri Oct 13 19:32:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 115827 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp1084769qgn; Fri, 13 Oct 2017 12:33:11 -0700 (PDT) X-Google-Smtp-Source: AOwi7QCqmLPg5SMjYsoCqa6KzylntgKatM03rJaMnURC/mV4elIKBo4PaqnQG1CGThXdXqeGqJRQ X-Received: by 10.84.224.195 with SMTP id k3mr2253636pln.403.1507923191740; Fri, 13 Oct 2017 12:33:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507923191; cv=none; d=google.com; s=arc-20160816; b=zOONG/MlFRJ4jtwkUKpbniIrR/o2JxNVw80e4/tlglSC3k4xOYRVyHszz5RsXVmjrn vQm2EX/IPzJXntCPsyLx4n4uSWqMrVzdIOVRHF/stuC5HPWMIwTqwOQvaTizW2ua22GA TKsV36BI4Arxvj8fy4etjTdkzl9BWm2rb7YFUlb90oxvQSvppTCjZkgE9rBc+rITACoK GW/80l0ECJBv6KhAJCK3WaRb729pomYrQvtubw8VVOikgzv3YZru6jRTqEXybSBf3j2W 9ydwFM3dwVeSVifndCd+5Mw8V1xILQZ2WbScxyOUx2mu8shzDMm+DIVCm9rLNqRnI4wN pkKQ== 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=ABknDcZejM8nOwymX7NN+Qc9cb/kSnsR6Ney2SpYkxd+8PKY9fhjEBUsr2gjHRGY1G Y7Q0ZWbfuP/rO0BLw+XN8HFyQjaTAhlwmP6c6tDKOQyOdQNio5rcrS5ugDgnY+cz9TO+ 3qyB4WV2TMozzkb09jHQ5n23aiLiulNkALaW1bqrphxjdb2b6fY7n7UV164Okb1dBYJG 5fUcPjc0Yfa9A4Sp56EpvRYXV6Gzx8xGA6KH/1HVGxlTff64ryc79WcqpzG3+fpxlQlT 3XSvKyAuM34qiDCyxP3rfVEMzS4PVaI0vnlNg39Q8y9c73Y7uo5ugk0dH6znfwelDmUh 4cFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@EPAM.onmicrosoft.com header.s=selector1-epam-com header.b=XtCXP8Nc; 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 s16si1031704plp.187.2017.10.13.12.33.11; Fri, 13 Oct 2017 12:33:11 -0700 (PDT) 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.onmicrosoft.com header.s=selector1-epam-com header.b=XtCXP8Nc; 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 S1753239AbdJMTdJ (ORCPT + 27 others); Fri, 13 Oct 2017 15:33:09 -0400 Received: from mail-eopbgr50048.outbound.protection.outlook.com ([40.107.5.48]:42538 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752684AbdJMTdF (ORCPT ); Fri, 13 Oct 2017 15:33:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=EPAM.onmicrosoft.com; s=selector1-epam-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=iZi+k3+kTkx4OCmJLCfoAMxvHXdbv50DUKvYGWWi974=; b=XtCXP8Nc0RYUxRN5R6Ku9XEnofZWAx3XoLFf2e5pgIWnCTxZuDEe+I7poOAZtM/XrO9BMebDIjgFL2DuCtsttZBmcA6Envcey+1PqW0LmJr4zt5J1C7BWo4O/Q4G+Uq7JTlVsluRcHE9B/4emKAe/u+TsoWAw7loDLePmgGgniM= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.52) by AM4PR0301MB2129.eurprd03.prod.outlook.com (2603:10a6:200:4d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Fri, 13 Oct 2017 19:32:59 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Fri, 13 Oct 2017 22:32:56 +0300 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: [PATCH v1 01/14] tee: flexible shared memory pool creation Date: Fri, 13 Oct 2017 22:32:31 +0300 Message-Id: <1507923164-12796-2-git-send-email-volodymyr_babchuk@epam.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> References: <1506621851-6929-1-git-send-email-volodymyr_babchuk@epam.com> <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> MIME-Version: 1.0 X-Originating-IP: [85.223.209.52] X-ClientProxiedBy: AM5PR0102CA0036.eurprd01.prod.exchangelabs.com (2603:10a6:206::49) To AM4PR0301MB2129.eurprd03.prod.outlook.com (2603:10a6:200:4d::14) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 77962f1b-ec8f-490a-93ea-08d512713675 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254152)(2017052603199)(201703131423075)(201703031133081)(201702281549075); SRVR:AM4PR0301MB2129; X-Microsoft-Exchange-Diagnostics: 1; AM4PR0301MB2129; 3:DKTUdZe4REvyvNHUkrfM1vBKYrSLRMSQyleTRsPeRpIySGwM5NPr+5U7U6iL1KU/BPRzb2kpPp8WOpXSw/UpfyUoVU9k0IDc0vH6XFSPCnX7AR/bRvs/zcmJAc+iejOvBnvafQKPU81LQj0uSF2tWIXjIoPQau7Poa1hHpqB2nhUi/4twl+l3E53hEQh/0PjLFE/VxEeImaJ8QZCDJMq83n6CQGcpWTWwg8+VGIThDqYXbIXrW4rhG76QcefxKoe; 25:tZ4OWKpCJz3A9D/jVcCwDXeSuYJXWrtfw+5Z/2KdgDjVPbmeip/FBXDFGsKaV7rzbyl/vvq8s+eiiSFWeJmC9RMQzKxTnHECIbIFYFq+pSktMvBebr7vFiPykE0uQBODMIXjwiOcC2TgrIS6GsE5aF6hu8IV/EEqQWG83+HzgEn+4A1c55nwDJ0T2gBA5zqQzVYVgb4KVJmUVdu5xxB2Y779/C3HBqAbTX8F0OXKugFAj1kucCf7sVrNqEMuKCTBxGDCL1jySqbMIlE14+wFM//MwpWLn/5HxiWXxua1UpkDX9GBEkecKuej/0zmIUm/HglNCEJndQ6f3DCK7XNmfg==; 31:eReQ7xkXjpalfgRmRej+YVchgEyjYeoygaHWcNoKQg4//wtogBeB+8IEteuUTeJLfwuAuXgaIPEhmy4TDEobkXKK7FPX5ciNzDh07lRuX3MI/UsU1A0klvarQXCQxnR/4zbP2rbQRyjbipG0O1i7PTv6IF1iP6SXy1zfpwZ58KG1AXoPV5CsSaHAw3+yYD1Unaj8kRHmR/Ii0uDb6RUBvUfiODzIKL3OAeTu8H0L9vU= X-MS-TrafficTypeDiagnostic: AM4PR0301MB2129: X-Microsoft-Exchange-Diagnostics: 1; AM4PR0301MB2129; 20:sha0DoPwHTxO/W+nAD+tAPFAetrcUSk64G70nyCCFRwbOQwZSNlVvTo76cxaiZFWScbUFG3ny/1jm7MJej6Y6N4aGP7fMvxAtvRw093FVJUkdA73VUknTYXxDzdrIAVEgR6h7qFDS75pJ1dUTS8SIpFWpORrGl1skUjQ34KSSXtAcHWHt3N0jRuxoojE5yvSVhmy0tAbf8KbI+zYqCrAgvdaRyyKGd9vjSMr7fZ0QTnyNuYEffL8RJ70tY/AOPlTrqZTDfil+b03F5PuOs6bip9K6Q8OufVVAlYyhSpHLCoCgDcphg+Mz2eaGQzAblOOPxKkG7jLX3zv171yZIsgGuOUzEaI9Px+6KiqWz39enOmoAVa2dbmyLxaoxhqcBKlAyhHDIWi4CWdfGYGkAwjQqm23K6wsOcWneo0zcyXsjheEyVlMfXA6RA1x5Q2A289pRjDKH3ciO3+FmR1p2GvMzWay0DjvZXw+k0BkAgD/YE+nB1t9eO7jSyaoHfp2tc6; 4:XGobxt1Wf3xRYZdPRssqBiRMpzk+vVHwQcQ/Ej9J31nFe/dvzSmlHx/wofSU+171/EC50NFXlw738PNDJzEu1jAFxzrUq8aDy32lXQcZDPA3ZIQazTiVfujMYT6sn48NRZoYk1fRAkHVgtagVwr6Z3bL320pHimU8EH9BQFnoD3TP5iPjAvh/xFSaB+ifocXSmi2PPcCZy2JQicLWw7IZh3dxRxP/BRgVsP/3VsV9e0X5YNlpvF1dU70DwDzH+5r X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(100000703101)(100105400095)(93006095)(93001095)(3002001)(10201501046)(6041248)(20161123562025)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123555025)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:AM4PR0301MB2129; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:AM4PR0301MB2129; X-Forefront-PRVS: 04599F3534 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(979002)(6009001)(376002)(346002)(189002)(199003)(33646002)(478600001)(80792005)(2906002)(39060400002)(4326008)(97736004)(76176999)(122856001)(5003940100001)(36756003)(42186006)(16586007)(316002)(101416001)(5660300001)(72206003)(189998001)(50466002)(6116002)(68736007)(3846002)(66066001)(50226002)(86362001)(48376002)(47776003)(8936002)(53936002)(105586002)(8676002)(81166006)(81156014)(106356001)(6916009)(2950100002)(7736002)(50986999)(305945005)(6666003)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:AM4PR0301MB2129; H:EPUAKYIW2556.kyiv.epam.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: epam.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: 1; AM4PR0301MB2129; 23:gXDfcQRN8ZnZzXFj3JgVTimB1/L44CbnXNoXIbkaxj/Ynzp7AooeY/mQP9yDgMNk2YR3mWb0upzzbXGnKmgVD8oNEFosQBxhiA6hVq5aY/RXUgW0kxvePF6Ca54e2aE+RITou+QeYdmR+t3snHqGvXQlpVh7H3gSyiCPq8zPolhuTqsbbcRUMLsczIIysUv4upO1y4k6t1O3Kbqen2RcP4BdudLLEWE1HG1aB0a6oJBMSxAcYkRtWIRe7Z2+R56xgn9nU3RJAqa+crjBANjmy3oh1V0IHt8YkOBnjxMvC32W34EQjZXn9eJkFeFnoyB0lmlANPc5/JDVzyM8SJ1/H/DgRwNsT3Ju1FZKkWdysxX2Nkft8360hhSX7xaSip0HFBntrX7B5NaYEmTiIV4gB4bM+F7C8v9UZhGU3JdamC+h6TsGoWQnInx7MiTtwyloswXXbz4NH1vKXllTS67uOZbwBZVKldB6E/KelpCJIolCxWoAW5JZXly+eM0e1bQuMUi/D7XghujYM68zL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MLs+RpI40mF2GWOHEeCxKx7/JWKBTer/i3zj7l0wMkWF5krD0KmicMpT4APpygRH8KgM25z6zUZYFR+VAkm0VIeGtNlXsHQBkWI8ByknVpFP/mU5lPD9WIblw==; 24:6O6Z9g9pvNHlVg8WRlqhxEFUTQKwLYs4UaOMa7GYxiMmU/YzwQ8mpViC8KE/asWv9kFG4AUxpn9E00HXHFpyT1hwEzs+5vFe5uCZ8BZ1YfM=; 7:ZHqf0ZgiIHTQMH78Px5owNHGcnCe6zl7d9r1F87+JfhFHTyIN6JUPGCi3vvf6Kc3eosIN6QKCF33emoJdiePaA4h0dpl1hAXa8i23y7PC/jmK0IR/YQYwu87DnAzdLy/b2hzRY/oUmlu39HEb3Zl4VqGtJimdLNSu5NYllRU/3IG5Tpoz1Slyi8jt+IYSK3vVUab+sRBw9C+JlEHJGNThsnJlAg8gsXp0MG+UFa6gAw= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2017 19:32:59.3322 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR0301MB2129 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 Fri Oct 13 19:32:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 115828 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp1087372qgn; Fri, 13 Oct 2017 12:36:28 -0700 (PDT) X-Google-Smtp-Source: AOwi7QB5UU1sVwd7mHi1g8HiKLEhorqPDg/B+MQwHsKU405GtRl7h/6c4USfrmEQMnHxsJ7iiy/R X-Received: by 10.101.86.196 with SMTP id w4mr2172156pgs.335.1507923388287; Fri, 13 Oct 2017 12:36:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507923388; cv=none; d=google.com; s=arc-20160816; b=siYZ+XFH89zybpZ0UiI67Fw66GY5C1Zb6HBmvSw17IQtLHOMwSQCNblneO5sUkdjHX mQ9mHGA2QxPk9m7fhClOUZXMo2XrH7gUg5FEnnkRXqf9C3CAvh/55V5g4YiR6HGTGNGB MsUKUAGlxAQ11EuvuSDtXuJQX+K0nBmPozMYyWk22JSPFPr1/lXm5+4deBA9CBFwrF4A NKbN3iKWrkkP+dggK/7rAPyYOy/Cf6s8g3fEx9/BRQZvb7WKMik+FxJtImnoUYpTOCwH 9itFT6lm5Kpqq+Dr0HVg9f4EuPzuIctNvyPuB3MpLjPC1dE7wfOU+MhO+qfbmYgfMqDy kGfw== 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=502BI4/cdc4/SQWikSTzPb5ItC4sBIJz0AORigqiUdI=; b=NDhUhJHuAhzQ8ni/9YmVwLSE3ORx5IpnduZyJwmQrnBPyxAKLJlFuHHBrd3NgVPQWY dP/IFfI4EwmLGDAciJUONZJHw1lP7i4Dm8n88h6ev2wFnb5IHPqmRfpAPgqGY2xGfCde Kt4OAu39B8In7DdOUA5XmTe3Y9wVn87Cm7b5O9PCtpXgCNnlE5vbMxwvscCQTkWSyqK4 2E7wMjHfbc+J57NgvZoHjKJxb9R4rgfJ2YmGDKq5iUEbWsASDv+KQQd6/586DLUfmP/H aZ7UIJCfQgLwQLuXYi8vf+n6jxzzbx6Tsk7muCMoeeQGLdbLc2917pw0JlIyeStJWtAL aF+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@EPAM.onmicrosoft.com header.s=selector1-epam-com header.b=EoOzx70v; 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 t8si1003162plz.145.2017.10.13.12.36.28; Fri, 13 Oct 2017 12:36:28 -0700 (PDT) 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.onmicrosoft.com header.s=selector1-epam-com header.b=EoOzx70v; 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 S1753419AbdJMTd3 (ORCPT + 27 others); Fri, 13 Oct 2017 15:33:29 -0400 Received: from mail-eopbgr00077.outbound.protection.outlook.com ([40.107.0.77]:23091 "EHLO EUR02-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753240AbdJMTdM (ORCPT ); Fri, 13 Oct 2017 15:33:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=EPAM.onmicrosoft.com; s=selector1-epam-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=502BI4/cdc4/SQWikSTzPb5ItC4sBIJz0AORigqiUdI=; b=EoOzx70vSwnZRkHD/2UATNqkRgO1JMKCfRskoWK0weRtMBLQk2ZNd437bC6fAbBnPMEetHBEJb24maj0pkqa/DSvYMNnQ873N5XkA2JaLbr+eVE4+6HBrESmdI/xJPSzrT0gg6BMbdQZPO/fdKTYeUV8zin07C7Z4q6U12DRMSo= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.52) by DB6PR0301MB2134.eurprd03.prod.outlook.com (2603:10a6:4:46::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Fri, 13 Oct 2017 19:33:07 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Fri, 13 Oct 2017 22:33:01 +0300 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: [PATCH v1 02/14] tee: add register user memory Date: Fri, 13 Oct 2017 22:32:32 +0300 Message-Id: <1507923164-12796-3-git-send-email-volodymyr_babchuk@epam.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> References: <1506621851-6929-1-git-send-email-volodymyr_babchuk@epam.com> <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> MIME-Version: 1.0 X-Originating-IP: [85.223.209.52] X-ClientProxiedBy: DB3PR0202CA0025.eurprd02.prod.outlook.com (2603:10a6:8:1::38) To DB6PR0301MB2134.eurprd03.prod.outlook.com (2603:10a6:4:46::16) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 46efc6b9-b857-4eb9-e409-08d512713b1b X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254152)(2017052603199)(201703131423075)(201703031133081)(201702281549075); SRVR:DB6PR0301MB2134; X-Microsoft-Exchange-Diagnostics: 1; DB6PR0301MB2134; 3:xnLSvVScB4r1W+hPKhPISrrvGin/v6xS+Gi1N+VgCYznQd3Jq14IHxkrdAxaeQNtMV6xKd6Ira5uXMD0B3zYRMmP1NX2WkL9TFitoIEzZPfWlq24aJMwb1Ykoo7D14pjEAYKyo05CG1nZbREDUTvwARfPO/UBc/tAm0/+2YEJ3zMrovO6jixEWD/ApCdkP9lILBo8v07OQOBFUNGXt8Ehh3sdHddLeX6qUYHAwv8UX2BmTZB6NkKuaa24TVuS3q2; 25:RKfAOYt30Nix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jvo4w4IQDysg1bKYfE978EOIpKA5CKJ7DkvLAQmpToG4lGCmlxrxuEsm04gou2ABZlo3ZzpDfT/; 4:CDzZ6RYVJG9zUyOTQ/MoByWJQZKqyxSaexiAe9i3byPOlJwZy4z7o4Wb8oRf27sHJPlPpj19teHBGlmY+niv2tPihRSL627fHQtKhI9p0N8FMAwinv8vs9yedYTtSHI5q9yeyGusUGbcxt00QsSPHXlsNznKp1XChrasqJo1F0wcdhiVsTXdzRcjBgNZxiBF/sWbHp+HK2e46D7UVIvrPH0SArDDRSCjAGvjHBsalcYZqdN5V1yLVNYmw6A24Nii X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(100000703101)(100105400095)(6041248)(20161123562025)(20161123560025)(20161123558100)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DB6PR0301MB2134; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DB6PR0301MB2134; X-Forefront-PRVS: 04599F3534 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(346002)(376002)(189002)(199003)(47776003)(4326008)(50466002)(5003940100001)(2906002)(48376002)(6666003)(105586002)(72206003)(478600001)(66066001)(106356001)(39060400002)(80792005)(189998001)(81166006)(97736004)(81156014)(8676002)(8936002)(68736007)(305945005)(7736002)(76176999)(316002)(86362001)(16586007)(122856001)(36756003)(101416001)(42186006)(50986999)(6916009)(50226002)(6116002)(5660300001)(53936002)(2950100002)(3846002)(33646002); DIR:OUT; SFP:1101; SCL:1; SRVR:DB6PR0301MB2134; H:EPUAKYIW2556.kyiv.epam.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: epam.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: 1; DB6PR0301MB2134; 23:YQcmF/t8s1r1AtsFP8G0Xt7s7o+oI+/uPuI8sKwtaw9exRSKZqbwTRu42SpsNyAWT+LELKdyMbPmQUBTDZF7C5rKC9dkLnikBSHAalWgBkzXfhjaMQWKRiv7c8x+0ltLG8ACA8zG9rnb//LR7hRFpjoHTDS9L3NEqRxHeon8EFrYB56gFzvRZCJsx36rHJHDCIdiFOQIQyiLe0jUX2YuDZfJAHbSExcqfA5x9I95IEzel7WDha7JQivY7ncuk8rPvntT7qnnW4oScoHttsd0WWTsqlZLU6Wn/jm4m4StPmAQWnVCY1AfI0B7X28JJ/1BHfn0Xoii0wCmghWZVMA3COA39vXwaHMvidKZ2dATylYuPhu0d4V9TrKs25b5mIrp/EgfYMGxuuzDhuvSnWflwPppJTgIznMPq61lLfzKwpQxMVYZ1E17rupdPz7/DOuahngsCKqCkebL/fymX+lYYAYegrAK79E+4A1E/UBO0fULj19vrSqcgQUcHyFk2Kd8IKmKzZ8HQG7cDJjNvXgp1kCap3iLXtO3JfOl9LkyD/+2HIxiBfEho7BRK1Zr77rjTAQqHmRGFmFFVTHk38lY1ifoxchAMNpn3zcjJltM2geBAqbIVQl+l3kP3NRi+CL4DPiMhlBozglFDgycM0dqvWb81pzLdS6rF3iai5FRFpRG0np83IlAvNfW0yeTeyehfyvh4X7XNLw5h4CHLPABSTuylXFLcShaA36vu/yrJzWZCn3g5QS4VGbX7IzMJ2BWEP2zjEqoh2zXDrkPr5tse0hjOcFL+GfTfCNxXtYQSAuOOD/5h+sUgmAZ6euZMZF+ST1sV6bCX+UldqGly+HbLp/eu1GCfocHhJFM3L7UzruxOU8XRyWvhx6f9DQUgwzOUhwU8WtJBh/0WChlHemyPAk2kgiSaD7rN2xttQtyH0QKmsatAL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osticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2017 19:33:07.0081 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0301MB2134 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 --- * Removed dev_err() in cases when this is not exactly an error * tee_shm_register() return ENOTSUPP in cases when registration of memory is not supported. * Fixed nasty mistake with sizeof(struct page) instead of sizeof(struct page*) * tee_shm_is_registered() made inline. In v1 there was separate patch that did this. --- 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