Received: by 10.223.164.202 with SMTP id h10csp4692032wrb; Wed, 29 Nov 2017 10:15:08 -0800 (PST) X-Google-Smtp-Source: AGs4zMYnNg110MEFHWpHfsQpqR7GZcv9hY6QksVvDLpdDaR/2cY5QGKQqzIexGGXKMtuj8C7xQzg X-Received: by 10.98.212.12 with SMTP id a12mr3847631pfh.164.1511979308891; Wed, 29 Nov 2017 10:15:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511979308; cv=none; d=google.com; s=arc-20160816; b=J0xtVs0sn+REYhNRfrFsyz4yu5C6OQ4rjdGC9AYq2KZsrqjwGtAOWoK8ggLrUlJDCb DsVzDiy82Rt7qRinsnIKrIYMeldNIV5Y0N8D4PBiFWPCwPw6ep9THZ4W6gsncLYyEyo3 VycTDdKmdfI1kXWVObz3rT1Xg1/rn+ArMFOYVFjLOtlLo+n3GHSOrQoJAIW9+5/StmWB VWoxQsi/bd1t/YIkmRsyrUsvi5bNYwtFMBLRiKlHx6zth8HkF7SzPgu3eYMKpsswnsTH ZIlUYT36eG6ZRICVUZMh0iwgnGqnEGKFhWGaaeNq3WiuYc6Cn061FE97hPOjD50E5uat WA5Q== 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=ak6EO8gcAl/9lVqfSJJJlnb7s/nUqpvchjyuvmjmAo4=; b=PU5aiqu+jmc4wTmW/deof1M1DWCpfFrRPLBxXgFzVkH3Cio7lB5IyoFF5fTTOsA63E Maxk2Uvtuwkaitr3Y96h8f+K0Xi5S04JETMQRYv3gpBXP8KG3Y9UcKaJNut0Do+Lniku /5OT4Stns7oy0ViIoQPwa9v6pfbNRcB0a1vUV4GcFEd3ys+ojm9yxGxccJ2Lb45yhhfe +HPTqaEvz+GDTlqvd5eRtZexJotP6i9IIcqcXAUZ19gVVPRfYEOysn9bKwSw77f6lcB9 R9o8PnDegESFxtenNE77KVfVpRXtxCy3YebOwC+lkxZ78K53y4EAZkwPnmwixLEjKets k1Jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@epam.com header.s=selector1 header.b=McAvVY3w; 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 w1si1667342plk.259.2017.11.29.10.14.58; Wed, 29 Nov 2017 10:15:08 -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=McAvVY3w; 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 S932662AbdK2MuV (ORCPT + 70 others); Wed, 29 Nov 2017 07:50:21 -0500 Received: from mail-db5eur01on0060.outbound.protection.outlook.com ([104.47.2.60]:23584 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754909AbdK2MuQ (ORCPT ); Wed, 29 Nov 2017 07:50:16 -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=ak6EO8gcAl/9lVqfSJJJlnb7s/nUqpvchjyuvmjmAo4=; b=McAvVY3wy4eJ39OLgzzyVZ/9rJ683CpZKvvlbuWJm1ktPjM4cc/T6EWFDr5AmuRF1vUL4kF2FWLvhjPMisLlo7NypdnuJrARvu0IRs/G5LX35BrO59cIqeORUnkf7+qlchOSnP0rmaaBjNTTEnCprTdgLOtspbiL7Q3/4N1EQ7w= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.56) by HE1PR03MB1769.eurprd03.prod.outlook.com (2a01:111:e400:7b72::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.260.4; Wed, 29 Nov 2017 12:50:10 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Wed, 29 Nov 2017 14:50:06 +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 , volodymyr_babchuk@epam.com Subject: [RESEND PATCH v2 13/14] tee: use reference counting for tee_context Date: Wed, 29 Nov 2017 14:48:37 +0200 Message-Id: <1511959718-5421-14-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 Content-Type: text/plain X-Originating-IP: [85.223.209.56] X-ClientProxiedBy: DB6PR0301CA0059.eurprd03.prod.outlook.com (2603:10a6:4:54::27) To HE1PR03MB1769.eurprd03.prod.outlook.com (2a01:111:e400:7b72::27) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c499c02a-a2e3-43fc-f12a-08d53727bab8 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(7168020)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603270);SRVR:HE1PR03MB1769; X-Microsoft-Exchange-Diagnostics: 1;HE1PR03MB1769;3:JIpLouv+xQzAE7wyPtANjqUtmSdhzuJpASE7yMO6u/6YRT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BROGKEVFok6Kzw8/mHRa4olE9x90ljlQJ9kKvuOo1ubEGHyeHwF9GahKMDvMdw8Za1R3q4lteRmDuIysciR/vpaP1WfiUkCMR4xqPALxHskojUa5/Alghw241cFyuMApIYPMYzpjI8UsqVVPk+P8QZX72DdEAWA8YKxFDbhNnKH444rUfomTwMK1luKiX5w2dwMfSg5NSsSsAO4Buc0YeOKieJ8VI5zY8Y3fPq5b50MN2Rdv81AQXC6B03qBaagu/hZNV7Zff0119AKYPYXaLH+FjouAlfRjMEKi91CKmIKgYBMaiak2YsWxuXPavu;4:ZNwmPu5t+Ms6qbDq6YgryIK0kJV9O1Pf4sd3Dju58DT/xa8WORdjlRwk0EVHhjVgUC5AWe0Y2+CuCEz7cxgEXYcEFBeGYBTjBDRldWqH5LMm40XAw24826GOYNdeW/25JTc5ghSUmJhf4KHSYL+QjzBI9Uvo60zEnMPEKwcHf5Vs5IVItGg62cFzNUkmubMZPo+vJ6P+wIWzdKQl8g3vmNpIrNuFpEMi/k23D7kL9ttPH2IzwXuwht1EeB9DgNdbfFl+P5w20SG8XV3lP6q56A== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(5005006)(8121501046)(10201501046)(93006095)(93001095)(3002001)(3231022)(6041248)(20161123562025)(20161123555025)(20161123558100)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(6072148)(201708071742011);SRVR:HE1PR03MB1769;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:HE1PR03MB1769; X-Forefront-PRVS: 05066DEDBB X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6009001)(346002)(376002)(366004)(189002)(199003)(105586002)(8936002)(55236003)(97736004)(39060400002)(47776003)(68736007)(53936002)(50226002)(107886003)(6116002)(66066001)(86362001)(3846002)(81166006)(81156014)(48376002)(50466002)(122856001)(106356001)(8676002)(7736002)(2906002)(33646002)(36756003)(6666003)(305945005)(5660300001)(50986999)(76176999)(189998001)(101416001)(478600001)(72206003)(2950100002)(6916009)(4326008)(80792005)(316002)(51416003)(52116002)(16586007)(42186006)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1PR03MB1769;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: =?us-ascii?Q?1;HE1PR03MB1769;23:AIgV+V/iZPR5IGZwdQiR7vkxyzlOZZXhJrrwZkHTQ?= =?us-ascii?Q?BxTMfXu7F4ga3K2Pq6gQlD9oXk6HBEXMKSTmwdARoXxOgcLcMkvl+egEU7kn?= =?us-ascii?Q?840CE99SbgxE4XMyPVPJ5BtR815jtPO6BhapuFiAMNIrmVWV8Q3K2JjXsx8s?= =?us-ascii?Q?9iRsdtvbSWIjgWp4wkw3WCPixKHrj1wZOcDSaGj9sUa4hfQACTIAborGLDz4?= =?us-ascii?Q?sxPNVfvZu5EMhgtHcN4B6tpNLFcM4b8g0DRYKGX0Cp04YwICTgxP1HjjJWev?= =?us-ascii?Q?dDFQFGPR8VlJf4i408hv8yOo0xuzmkWejlFgTgMGbEfTNrXzVY5358fzfk6s?= =?us-ascii?Q?s4ZWE9r9LD1QzVdlCE/tTLiWdzKPPkdbMP96MmRvwkjJKI67Foy/mVtrqjeR?= =?us-ascii?Q?sJkfGxcqa4+a84+GKZXynpItFvZNOtcTI8rJF3i/wTC8YVLsrFeRD4G55Fwy?= =?us-ascii?Q?CvSktOoTb4JxzmurI0b+CkkoM6KCXA4OL01KfgHQQ1lJJ7jz9Z0Ws02Dm6Cs?= =?us-ascii?Q?pzlFXJCBJgP2E8rc/yvyGY6R8v+fXnLiSwlZfc46KcJO5/ZPZo0caEV1p+fK?= =?us-ascii?Q?jYtzIdrQ1FyO3GT+QzSGx2bXSRLpLCt3yq5ciME5Br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VKBCmb+JIahq46I4OMrRBJjay8bkwYqMunrYXy+UMtv/UTJ6aoK4GPNpGakOEY/lxbjv62lB/aFPuonzL2Ul8u+6vthNgBmwoQOP+xeqxFFHDucu+9/mkC9UO4VlIKIpCEmFGKYPEG+rj4aeEStvH4HhIc1WDmtmBFv2sdVI+eA=;24:Xa1Uj+NVgz/3mu7rSXdh2hGQ3JZdUe12AWxo+FRUozmVn2V890MBIC5Ce2m36qzc/92jU2fFckJMMINPv44EZxQm3xkZrD/G0nSLhynwvPM=;7:9o2HkMGqrNJCFUvpyTfA0hni5vMwgUqUfX3LwczCOVICMTFBleW/2q4Cpz+N9rbUQ6ezV2fMp2IWXlKsyoySo7oXhxxSh2N4/ggMxoF2jPDF7RdSMNOKpEDq3jWiE/n//mTwX2O/Jk8Ne4ya48o9/rez/kJDhmj/tqXab3YKOl67KaXXcmiCLbrLXE/ehlkYCJcRtfnl6g1p+vNRPsDSWMoIrkDH128WI8lDICzX8+ZmoMHVVluB1SEeghJxM8m+ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2017 12:50:10.9508 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c499c02a-a2e3-43fc-f12a-08d53727bab8 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR03MB1769 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Volodymyr Babchuk We need to ensure that tee_context is present until last shared buffer will be freed. Signed-off-by: Volodymyr Babchuk --- drivers/tee/tee_core.c | 40 +++++++++++++++++++++++++++++++--------- drivers/tee/tee_private.h | 3 +++ drivers/tee/tee_shm.c | 7 +++++++ include/linux/tee_drv.h | 7 +++++++ 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 295910f..3d49ac2 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -54,6 +54,7 @@ static int tee_open(struct inode *inode, struct file *filp) goto err; } + kref_init(&ctx->refcount); ctx->teedev = teedev; INIT_LIST_HEAD(&ctx->list_shm); filp->private_data = ctx; @@ -68,19 +69,40 @@ static int tee_open(struct inode *inode, struct file *filp) return rc; } -static int tee_release(struct inode *inode, struct file *filp) +void teedev_ctx_get(struct tee_context *ctx) { - struct tee_context *ctx = filp->private_data; - struct tee_device *teedev = ctx->teedev; - struct tee_shm *shm; + if (ctx->releasing) + return; + + kref_get(&ctx->refcount); +} +static void teedev_ctx_release(struct kref *ref) +{ + struct tee_context *ctx = container_of(ref, struct tee_context, + refcount); + ctx->releasing = true; ctx->teedev->desc->ops->release(ctx); - mutex_lock(&ctx->teedev->mutex); - list_for_each_entry(shm, &ctx->list_shm, link) - shm->ctx = NULL; - mutex_unlock(&ctx->teedev->mutex); kfree(ctx); - tee_device_put(teedev); +} + +void teedev_ctx_put(struct tee_context *ctx) +{ + if (ctx->releasing) + return; + + kref_put(&ctx->refcount, teedev_ctx_release); +} + +static void teedev_close_context(struct tee_context *ctx) +{ + tee_device_put(ctx->teedev); + teedev_ctx_put(ctx); +} + +static int tee_release(struct inode *inode, struct file *filp) +{ + teedev_close_context(filp->private_data); return 0; } diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 2bc2b5a..85d99d6 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -73,4 +73,7 @@ int tee_shm_get_fd(struct tee_shm *shm); bool tee_device_get(struct tee_device *teedev); void tee_device_put(struct tee_device *teedev); +void teedev_ctx_get(struct tee_context *ctx); +void teedev_ctx_put(struct tee_context *ctx); + #endif /*TEE_PRIVATE_H*/ diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 177e341..b48f83d 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -53,6 +53,9 @@ static void tee_shm_release(struct tee_shm *shm) kfree(shm->pages); } + if (shm->ctx) + teedev_ctx_put(shm->ctx); + kfree(shm); tee_device_put(teedev); @@ -187,6 +190,7 @@ struct tee_shm *__tee_shm_alloc(struct tee_context *ctx, } if (ctx) { + teedev_ctx_get(ctx); mutex_lock(&teedev->mutex); list_add_tail(&shm->link, &ctx->list_shm); mutex_unlock(&teedev->mutex); @@ -252,6 +256,8 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, return ERR_PTR(-ENOTSUPP); } + teedev_ctx_get(ctx); + shm = kzalloc(sizeof(*shm), GFP_KERNEL); if (!shm) { ret = ERR_PTR(-ENOMEM); @@ -333,6 +339,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, kfree(shm->pages); } kfree(shm); + teedev_ctx_put(ctx); tee_device_put(teedev); return ret; } diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 5489c38..9cd4e5f 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -42,11 +43,17 @@ struct tee_shm_pool; * @teedev: pointer to this drivers struct tee_device * @list_shm: List of shared memory object owned by this context * @data: driver specific context data, managed by the driver + * @refcount: reference counter for this structure + * @releasing: flag that indicates if context is being released right now. + * It is needed to break circular dependency on context during + * shared memory release. */ struct tee_context { struct tee_device *teedev; struct list_head list_shm; void *data; + struct kref refcount; + bool releasing; }; struct tee_param_memref { -- 2.7.4 From 1581172238241176177@xxx Fri Oct 13 19:35:53 +0000 2017 X-GM-THRID: 1579807807429777945 X-Gmail-Labels: Inbox,Category Forums