From patchwork Thu Sep 28 18:03:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 114464 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp1032541qgf; Thu, 28 Sep 2017 11:04:56 -0700 (PDT) X-Google-Smtp-Source: AOwi7QAX/TZ9HnFrVjXhy4SOPJ7mMoCPfFJW1ZonTYtgz+RS+B51Ad6+qgq/RGN+j59PJ5RHU6fv X-Received: by 10.98.34.219 with SMTP id p88mr4972646pfj.121.1506621896527; Thu, 28 Sep 2017 11:04:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506621896; cv=none; d=google.com; s=arc-20160816; b=IvvybIMd6BN1a/5izZ7kxUs9/efE07jRtS4lZnqfvKHsCLHkt7QjH4DJBV31mAb67m QqJulw/Q5W40JK1SJUcQs2JKBGez+krwU0bfl3GxfmiLF/j1kt7zLCg5xlAomgbGTqtH KXgUaQjJuZMTJMdiIX5xCoDs3GIVP2KBUSiEnnBJhhvzxZsuhQniD250mte6yZdng9z6 1Ut0dkcsPimXWjsrXHXdrN2V8AE+twrBi2VEa7AKOy0Pn7Z43GrrTTmyrB9V7QH6WXG0 jW42TkS8cewsgw9g+EdFp4K0HN2ujcYvrrId3BrhKRl/IGhpNmE4bdOuZt4xXZM2R+0v 675A== 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=JCcyhZHvw+PVcYUMxC2nzwBz0ht8WG1vHiJHRRupmjY=; b=JQP6qSt6JDJFwQu9g0U6ho0qa5JWcvJ26eas5m3N/WxsqqKXmAHSx0Jemaaig+fWUU PHPvIQD9EQ4pD0yKmmvZl/yEHZDWuWOgHKmiqM7LpDBsmGBI4mL0MxNbHIHQZkNFt0qO kMQFCFlI1hnemJPZ0DFNgNH7avmyDsQm81ENbydZjIzRJQk27gNYvUuzae1IHISEZ05j qj5rlLvdwbXng5jwc/iQhEqIlpQMsPwVMfLuP1HeT9vAqQ3sWubiy0J9kckcBZsB1NId Jh4mzOSF2s0tKEQEvP7Ed7VUH6H0lRrXFEhhsLbncgJ2cyZYeKts5FjegSblxSbvoG2U 6s5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@EPAM.onmicrosoft.com header.s=selector1-epam-com header.b=KuXEZ3dJ; 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 u196si1838980pgc.648.2017.09.28.11.04.56; Thu, 28 Sep 2017 11:04:56 -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=KuXEZ3dJ; 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 S1751946AbdI1SEz (ORCPT + 26 others); Thu, 28 Sep 2017 14:04:55 -0400 Received: from mail-he1eur01on0073.outbound.protection.outlook.com ([104.47.0.73]:4983 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750902AbdI1SEv (ORCPT ); Thu, 28 Sep 2017 14:04:51 -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=JCcyhZHvw+PVcYUMxC2nzwBz0ht8WG1vHiJHRRupmjY=; b=KuXEZ3dJ/cRE8q/58oWr2E2b9GXzDG56DTZpvrb+4dOmqeb19JW9atSyLDkQJDnW6Igzatu5Jj+8vbiodUE7JVbyQJXstqJ/x3l3RCRVzYegtnpXXTefD2iJDEfSQ1vUJoSVNFD/HXA+aabSSL9JmBFiY3/dR3KVgy6eV5UO4t0= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.59) by VI1PR0301MB2142.eurprd03.prod.outlook.com (2603:10a6:800:26::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Thu, 28 Sep 2017 18:04:46 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Thu, 28 Sep 2017 21:04:42 +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 Subject: [PATCH v1 02/14] tee: add register user memory Date: Thu, 28 Sep 2017 21:03:59 +0300 Message-Id: <1506621851-6929-3-git-send-email-volodymyr_babchuk@epam.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1506621851-6929-1-git-send-email-volodymyr_babchuk@epam.com> References: <1506621851-6929-1-git-send-email-volodymyr_babchuk@epam.com> MIME-Version: 1.0 X-Originating-IP: [85.223.209.59] X-ClientProxiedBy: AM5PR0601CA0047.eurprd06.prod.outlook.com (2603:10a6:203:68::33) To VI1PR0301MB2142.eurprd03.prod.outlook.com (2603:10a6:800:26::15) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 77d0da43-4038-4fb3-569e-08d5069b67b2 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254152)(2017052603199)(201703131423075)(201703031133081)(201702281549075); SRVR:VI1PR0301MB2142; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0301MB2142; 3:pP0DC3JYfrJQB26+q+Fmkzf0+OGVuxKxhs5dyqnulTU//gsDOCSphv3gH6H1wogupxWIWnDGugXfPKVpMPnJO/D81htWfOUCmDrgirbP/9Z/U5t2vuRKnXvfA2nCYo8HDW7brSOATcxSgL34seJ/yU13gOR6uUb0bbNA6EWdjRhLfj5pfIjuZ+pVFEQMy9bsD2Glkpau7z5f5d7zIz4z1J5vo6rnKSmfr3ieAacf0Ip6xU+vE5Cv/Pb1Q81zX7+x; 25:oUgpRAJ+zoPzmBQbH9C+k1LpZmh5115+C6Npm6GOvNdQxI5F49dNIFlcBXU+KJsNbrMbu84TVol1iNkRMo8vbeni3whrqf2uBN2yZb6as1PuJCbzfWZ0gdFWSWc7V3Pj2BDt1z1ztNJES1350hEKeWf7U8XVim1aJNltnkGN74B9CYgGxC4ztdQEO1n94/QwVRK9mi4qlTXQ7tM/L/y5kYatk72eB4IwHkGz6dVaL5h7st8vLIk6biyBx1PIeY1YHJaxuCRmkA+hNvHw8Q1/iyBZxqNHJB3qmu+GzwzvJq39kDIJaELpnDlCSvfjT6CrfFvmCojA27qGCeq8lMFk5A==; 31:r2aQOvtXPCzPcZ0NunToAq1no0pQ4iD75HbNls+hXSthLWR9Uabl2IiLZekKfnX/fATysYFKBiL6dQTsCrV73wmHSR6b/Wm8o4jUos+o4L5wrOh9+9oILoW2XG4783tG9/aVLS0g9y+nu+WHZ3Ld5tfvKy6A1/FkD+KqE5ITTO/+VTulgio30hYKXwYOQd8zD1qeJwBYgMHtwbtjukJ0JxacE3U9V98+qO2CBHN4YFU= X-MS-TrafficTypeDiagnostic: VI1PR0301MB2142: X-Microsoft-Exchange-Diagnostics: 1; VI1PR0301MB2142; 20:zIbisnYWDu6Pg6AznzWZFVwZ35tTAIz33w8GSVzJI5bHuoc5EJSvorUf7KJONKCQ2Ws/RZ1Ma0LqnjzSV089gEsOnUBfqqx+xw6ftNm/2unLwqNCDyHoAxZC7HUR2vglfP0Zd6K5664rcYw26katCLbtVdj8MM7mriF4YqMcXpZJBrshwl6ya1QmHhBm9JojAB5xRxc+ccGminoyPOVn67/2NvzFPS0A5eSQCaru2HVx5yEXoziAO82+n1+gxeq1BnBOHs7Fn57uMFvsg/7kc52Mmqa2Gq8xG9uw629taO/tKUp0t5zKGwJlQhZg3tbDH/94FIb/LzWbVvEBbYueloxrn248TKhVQdbJOeB14waqL/XAGRhw5BrRgJr7SellwGHYx+oGAVJgrkVTgXAXs0Ggvr3NkZDEkTFTi5IYBpUJsbDCs1GVZILXAd6ds/X+FKHl9hiOYlhMK/vhMjxPAKZs+QWq1Xqzsgss+PEMhP7kPtyg+fACp4uQqcFNz/ww; 4:8u/Rcsp5KqBetOQ9paCvvzfauSDNfW5hEFf5n2QL7+0dD2vTDQSzixjizBWRb6W4S4Z6JBq8uRDRppJ7KS7YZ71YI25o3MzcSIGGAGhMls8EJ/rs5MKrYMk8MiRP5qLQfiRamGRec2nkB/rwVcQwWYeWBCxqd6OnreJ3EABrrRrcyCfBf6f8RIIPIAVLXaYJ6r/t+YrG1v8qQKiSEv6K4j9PRiJXsBa4fXT6FDVE00QdMXhrw/ViVlBNXBBONQy9 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)(20161123560025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123562025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:VI1PR0301MB2142; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:VI1PR0301MB2142; X-Forefront-PRVS: 0444EB1997 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(376002)(346002)(189002)(199003)(6116002)(97736004)(42186006)(66066001)(53936002)(80792005)(316002)(16586007)(5660300001)(86362001)(50466002)(122856001)(4326008)(3846002)(478600001)(47776003)(72206003)(6666003)(68736007)(81166006)(36756003)(105586002)(50986999)(5003940100001)(81156014)(8936002)(76176999)(101416001)(106356001)(50226002)(8676002)(189998001)(2950100002)(39060400002)(6916009)(48376002)(33646002)(7736002)(305945005)(2906002); DIR:OUT; SFP:1101; SCL:1; SRVR:VI1PR0301MB2142; 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; VI1PR0301MB2142; 23:7zsLZn3Uy3Pt5yobYma7iE9Rq4pZ1nrjrhcua4cH/3sYLMh08aW3tlRx4/usVx2Tk8mM7dwv3WQ5Gh/yUErM63RiVXVEw5SlPt/eKNNTjY9sDWZhXTg8BO+Ls5M0UDGq/12Fgt+BqHD8TCTtKgAXXBfBTUUepXR7h9qpLtbvlOwqv9OcFXDSjym82ZYUrh7VvZzE9Jhevm8Bn7BHGGyJOEIKmrS1RTmrH3GRHb4vj+MzRknrzksPfwqwb99XO+FS4H3hoOirGiJz5KqanWimLZU5/kb3MLbOWBcXoWfNUGBMFTrzMxtkR4tFFCnQZ1Jdz/KYiA2YsjXTmeKjFZ4wbYA5UYVcQePg/s2ZMj5MAXns6Lm6VD/mbpN94330Mw7FlJ8Q3LbJ8nvZbnLzNh31Bj85eRkf7uGOhkEzUBLRjG5jlsfQshGAIeW4jegBxXzuPwOOCCfIc2+tY4tWvNulGvVWimty/H9fxGR2HqHYAwHEy88uJGrmcC3AMs/Xc3QzxTn4gS2euYpclRB7e/Kw0zl0jLvk4V4EgSBz+XppgVaU8ij4zYZOZVnNWBOWs4w3kfsDpBsJBChGj+UM95aHrTh2QTW1xInlGUKTwqkEBuhEIcIZTJDo/PEOjEMX8L+Qas5CCWKkh9bAA6Tmh48Od9ucO8osEZcE6ZRkr2TWSszKUN/sY+62qFHoUUMCDlHxvLD2YAeM9fNbxtPpThkbbtC0FAcZnNRQOWb02SD73k3RFSK2gusVH3ESp27l3bN6ud4nzfQqG2O81XrjzYnomFEXCsVCNe5ymDkkHaplcZ0/hDXykWUgs5WuovhFxvH5VcxOLyIgo35NYt+sb4d57tjv5y4xUKLuB/KllIWPYBDxZe2J4VOjBiLf7iTJT+IX65Vfm7FL4NHlVpWSeO7e77n9nWNA0PUKj0JDwQqzBC0yvpcDwyoq7Ymh/TOSrIVAWC8P2qfRYH/MP4/PgG5mrJ8UhyyjmpWvrkuppqXYPHPCsJfuGlE2Md2mp0PTEqu39MkczCHxQQR9+PddE4IkKeQS0BMmNX6Q7lM5yd/aYwPUupRlyCLiR6D3VZPShQN8 X-Microsoft-Exchange-Diagnostics: 1; VI1PR0301MB2142; 6:Npm/u9gYkMt7ClIs1tmVcxWgbS8Ttmvfp78urFwvVZ+ei0rkFy6P42tcP7tt5pW1P0NfhM5isiwPL+WD3ef97aJVgdKyGfK2lcv88p5hq5KFpYLEJGFa9qbeoS3YsTcSey+HCL6Ku8JvlZT7CJF4sleprnZ2CNN8dQtjTdpQ6hA+BU3vHwbH6e373vhssqYpyaWDOrhhBSFItuRjpD6qaoSZczlwyzoAA4u/HkkAbEW33JZ8kpDE1r7xiaDeNAD1oIZSa8Ok9on2Gk5lp8YdDZ52N3AhNJTt4qJ6cAfIJRqqlRpOjWQOZYy4bZqsfUHrpvvNrd0rS65lxPsCjfxl3Q==; 5:xZjZeSUBMnp4onGzaSRshNjSF3xnosf+Y4ILJI8Qmg3lVR+7XhP2+ZuOmSl6spflIU5bksKsporfy/mq5UjFKuDUN8gLeesWO760hxrEVdqhlf6TFCOIOnhDgEKv069R0zeDBfN/kFduupg3pUS/dw==; 24:qgPAFInOmSkB8JlvlwHuHqWXg3RC5+ZfiUxaRrVGWRJJxOHdV1cNrTzDuNXseekkeIT4Fkd7nGoegj84vOB8Q8CnTWvLvEJ7+qXM+C8uxf4=; 7:YSPLuY+WwQNgOCQzFFYWYUIhoZ2LFB/lp9YdI1EeVEcBlXN0TpPdYY+v2gFWNsQA/4S+tGxuWISFMA4pl8aG9/YHEXY0LHsxint1qeoo/Ac6xYmgSDIyvsLCqStiA1IdUguX4OHjOjYKM0wF61L58IcZ/PTz1lCdDg9WFqoafDQWNaB0QmrNTVTxdL3dQjqBKdeyUt1MzKk/Hj2jPN1Y8ztU9NG/aCeL35smW9G++co= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Sep 2017 18:04:46.7700 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0301MB2142 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 | 210 +++++++++++++++++++++++++++++++++++++++++------ include/linux/tee_drv.h | 44 +++++++++- include/uapi/linux/tee.h | 30 +++++++ 4 files changed, 295 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..1ed708c 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,138 @@ 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) { + dev_err(teedev->dev.parent, "invliad shm flags %#x", flags); + return ERR_PTR(-EINVAL); + } + + if (!tee_device_get(teedev)) + return ERR_PTR(-EINVAL); + + if (!teedev->desc->ops->shm_register || + !teedev->desc->ops->shm_unregister) { + dev_err(teedev->dev.parent, + "register shared memory unspported by device"); + tee_device_put(teedev); + return ERR_PTR(-EINVAL); + } + + 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(struct page), 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); + + 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 +344,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 +384,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 +406,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 +434,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; @@ -335,6 +487,12 @@ struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id) } EXPORT_SYMBOL_GPL(tee_shm_get_from_id); +bool tee_shm_is_registered(struct tee_shm *shm) +{ + return shm && (shm->flags & TEE_SHM_REGISTER); +} +EXPORT_SYMBOL_GPL(tee_shm_is_registered); + /** * tee_shm_get_id() - Get id of a shared memory object * @shm: Shared memory handle diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index e9be4a4..49d6361 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,11 @@ 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 + */ +bool tee_shm_is_registered(struct tee_shm *shm); + #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