From patchwork Tue Feb 13 14:52:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Balint Dobszay X-Patchwork-Id: 200471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp595115dyb; Tue, 13 Feb 2024 07:03:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWDCvd7BDcjqZ4KeCvtaSHSIvIkVOWZY4b06v3CRpI6KX/5v6hzxofsUUgzuZq9lARibf7rQ997ddFAKhhCT2rPL0OuAg== X-Google-Smtp-Source: AGHT+IHBtLdW2RluNIQTpvLxgSspah6r0ua+Yn9AHFGhne9kKOjNQu3/xNQ1L3Xw56mO8js4xcFg X-Received: by 2002:a05:6402:12cb:b0:55f:2821:553a with SMTP id k11-20020a05640212cb00b0055f2821553amr7134852edx.13.1707836628574; Tue, 13 Feb 2024 07:03:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707836628; cv=pass; d=google.com; s=arc-20160816; b=IrUIqiQRCREzQ5VVsdL3zMoStIITyOXKza2YMia5qPUVfTb7UPh3mZPgmXt89qPI+g sOi5SHiRt1ZHqjxLhPkZITDC+Jk+JA1Lx8T39E1FXYH+D8tnM86fj2dVvmFJ7e8X0gVF Q1dPbi2WMzyqzUTl8C2KPsKCyAtJnSPQol0v6MlCckALWZe+GuanZbxlwklP7/2qa8OC 0IOaar0pY2PFQjm+7e3EKpc7HxYUaYmK0vF07ZeDzY0NH15rrkAkP5MwmgfKRrsorp0w JYJNrwgHpS/oZSivCZ2bbZTqqdlVrhjZs58jSRfbgINYeHcAqzIwUCVeJSGARITvl83T eklA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=/BdlHhT9XErUuDeiqddIc0+1eSy757Lxd1uKRrh0Se4=; fh=28sJDM5XlrdsL7fpvQ5AR1preMA49qw2/UE1L4r0JGA=; b=lahb4aGkMTkeV1arWzZNPEIBz6aR1qLY186eD1ShyVTi2+Nz68GayMVDM/SkzTVNsi CcIX5U1e/g4qC5tjODfpC9vIqvlXoU4zqmP/pJ2dIW0cgfPiZ0R80md0q2NpdQxlcVdg +BNDXgew6PZAJhoTSWbfD65uIVGvhCKWaiVL3hrmxV5A624t1P61S/4/1E0T91smglfI 05Q05yydvVavYeiL+2gh7ztUlL2jxZCn8kfMOL3lUoeaXTk34jg3zpQJ1Nhx/u1MWkqB AA7zXjXLFj0LLTI4b4IzfE7J0XMBnxPfSXh73K9q2FWNl3Twlb5gA4dwgQSAFPn+rPur VAug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-63723-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63723-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCVEw2vJ0kYn102uqGrBB9DeGVFQM8xgerv7np9vJnAeNF+/sfu9aMC1tJfUMyRkseZhiGkByVk30U9UNQy2lLhc5sN8gw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g12-20020aa7d1cc000000b005617af3bf43si3231627edp.19.2024.02.13.07.03.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 07:03:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63723-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-63723-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63723-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 15B791F2B2D1 for ; Tue, 13 Feb 2024 14:54:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 063D55DF30; Tue, 13 Feb 2024 14:54:03 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A7E526AD4; Tue, 13 Feb 2024 14:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707836040; cv=none; b=tzQKlvxdyrJrOrIMrH5LekrdfJGict0+E+o0UvjbpOH2pkPqjYTmN5umTpymZuWnfMZ6LwfLS/TKQEDuHitCXDsn4uA9F3f2nuN1mErUPZuVkUzPIKZ8bPxhNJbUR7/hrJHUD8AijcLEETGtdq0kwZp18qD3k+nRiZKHpFbiM5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707836040; c=relaxed/simple; bh=9zMxIiodTzgE1GyrP2I4rb5uFKRlL7/UtTG8n78T8pw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=T61m1ssann9tvljO/9cXzQmkJpuvCPS+E5euQ3d4JZUkFn8ij1Mxd+59WO+VaiAy5Jl3PWEeLxBS2QSLNMje3kWkFa9RJR/uzqCYYV+GIWrQ9PQj+NKS16PlqCqNpvUiJdrw18wpQt/bWzg/Xvu/Ly5IhkqVqDMWHV5TgoUM9b8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0E948DA7; Tue, 13 Feb 2024 06:54:39 -0800 (PST) Received: from mango.localdomain (TVHN20WD9G.budapest.arm.com [10.45.25.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 4AD413F7B4; Tue, 13 Feb 2024 06:53:56 -0800 (PST) From: Balint Dobszay To: op-tee@lists.trustedfirmware.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: jens.wiklander@linaro.org, sumit.garg@linaro.org, corbet@lwn.net, balint.dobszay@arm.com, sudeep.holla@arm.com, gyorgy.szing@arm.com Subject: [PATCH 1/3] tee: optee: Move pool_op helper functions Date: Tue, 13 Feb 2024 15:52:37 +0100 Message-Id: <20240213145239.379875-2-balint.dobszay@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240213145239.379875-1-balint.dobszay@arm.com> References: <20240213145239.379875-1-balint.dobszay@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790796500695196515 X-GMAIL-MSGID: 1790796500695196515 Move the pool alloc and free helper functions from the OP-TEE driver to the TEE subsystem, since these could be reused in other TEE drivers. This patch is not supposed to change behavior, it's only reorganizing the code. Suggested-by: Jens Wiklander Signed-off-by: Balint Dobszay --- drivers/tee/optee/core.c | 64 ------------------------------ drivers/tee/optee/ffa_abi.c | 6 +-- drivers/tee/optee/optee_private.h | 12 ------ drivers/tee/optee/smc_abi.c | 10 ++--- drivers/tee/tee_shm.c | 65 +++++++++++++++++++++++++++++++ include/linux/tee_drv.h | 11 ++++++ 6 files changed, 84 insertions(+), 84 deletions(-) diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index 3aed554bc8d8..9390f21f9902 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -17,69 +16,6 @@ #include #include "optee_private.h" -int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, - size_t size, size_t align, - int (*shm_register)(struct tee_context *ctx, - struct tee_shm *shm, - struct page **pages, - size_t num_pages, - unsigned long start)) -{ - size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; - struct page **pages; - unsigned int i; - int rc = 0; - - /* - * Ignore alignment since this is already going to be page aligned - * and there's no need for any larger alignment. - */ - shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE, - GFP_KERNEL | __GFP_ZERO); - if (!shm->kaddr) - return -ENOMEM; - - shm->paddr = virt_to_phys(shm->kaddr); - shm->size = nr_pages * PAGE_SIZE; - - pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL); - if (!pages) { - rc = -ENOMEM; - goto err; - } - - for (i = 0; i < nr_pages; i++) - pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE); - - shm->pages = pages; - shm->num_pages = nr_pages; - - if (shm_register) { - rc = shm_register(shm->ctx, shm, pages, nr_pages, - (unsigned long)shm->kaddr); - if (rc) - goto err; - } - - return 0; -err: - free_pages_exact(shm->kaddr, shm->size); - shm->kaddr = NULL; - return rc; -} - -void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm, - int (*shm_unregister)(struct tee_context *ctx, - struct tee_shm *shm)) -{ - if (shm_unregister) - shm_unregister(shm->ctx, shm); - free_pages_exact(shm->kaddr, shm->size); - shm->kaddr = NULL; - kfree(shm->pages); - shm->pages = NULL; -} - static void optee_bus_scan(struct work_struct *work) { WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP)); diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index ecb5eb079408..ee11918a2b35 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -374,14 +374,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_context *ctx, static int pool_ffa_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, size_t size, size_t align) { - return optee_pool_op_alloc_helper(pool, shm, size, align, - optee_ffa_shm_register); + return tee_shm_pool_op_alloc_helper(pool, shm, size, align, + optee_ffa_shm_register); } static void pool_ffa_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { - optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister); + tee_shm_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister); } static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool) diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h index 7a5243c78b55..a153285a1919 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -283,18 +283,6 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); int optee_enumerate_devices(u32 func); void optee_unregister_devices(void); -int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, - size_t size, size_t align, - int (*shm_register)(struct tee_context *ctx, - struct tee_shm *shm, - struct page **pages, - size_t num_pages, - unsigned long start)); -void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm, - int (*shm_unregister)(struct tee_context *ctx, - struct tee_shm *shm)); - - void optee_remove_common(struct optee *optee); int optee_open(struct tee_context *ctx, bool cap_memref_null); void optee_release(struct tee_context *ctx); diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index a37f87087e5c..78f4c8c43790 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -592,19 +592,19 @@ static int pool_op_alloc(struct tee_shm_pool *pool, * to be registered with OP-TEE. */ if (shm->flags & TEE_SHM_PRIV) - return optee_pool_op_alloc_helper(pool, shm, size, align, NULL); + return tee_shm_pool_op_alloc_helper(pool, shm, size, align, NULL); - return optee_pool_op_alloc_helper(pool, shm, size, align, - optee_shm_register); + return tee_shm_pool_op_alloc_helper(pool, shm, size, align, + optee_shm_register); } static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) { if (!(shm->flags & TEE_SHM_PRIV)) - optee_pool_op_free_helper(pool, shm, optee_shm_unregister); + tee_shm_pool_op_free_helper(pool, shm, optee_shm_unregister); else - optee_pool_op_free_helper(pool, shm, NULL); + tee_shm_pool_op_free_helper(pool, shm, NULL); } static void pool_op_destroy_pool(struct tee_shm_pool *pool) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 731d9028b67f..641aad92ffe2 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -202,6 +202,71 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size) } EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); +int tee_shm_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align, + int (*shm_register)(struct tee_context *ctx, + struct tee_shm *shm, + struct page **pages, + size_t num_pages, + unsigned long start)) +{ + size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE; + struct page **pages; + unsigned int i; + int rc = 0; + + /* + * Ignore alignment since this is already going to be page aligned + * and there's no need for any larger alignment. + */ + shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE, + GFP_KERNEL | __GFP_ZERO); + if (!shm->kaddr) + return -ENOMEM; + + shm->paddr = virt_to_phys(shm->kaddr); + shm->size = nr_pages * PAGE_SIZE; + + pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL); + if (!pages) { + rc = -ENOMEM; + goto err; + } + + for (i = 0; i < nr_pages; i++) + pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE); + + shm->pages = pages; + shm->num_pages = nr_pages; + + if (shm_register) { + rc = shm_register(shm->ctx, shm, pages, nr_pages, + (unsigned long)shm->kaddr); + if (rc) + goto err; + } + + return 0; +err: + free_pages_exact(shm->kaddr, shm->size); + shm->kaddr = NULL; + return rc; +} +EXPORT_SYMBOL_GPL(tee_shm_pool_op_alloc_helper); + +void tee_shm_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm, + int (*shm_unregister)(struct tee_context *ctx, + struct tee_shm *shm)) +{ + if (shm_unregister) + shm_unregister(shm->ctx, shm); + free_pages_exact(shm->kaddr, shm->size); + shm->kaddr = NULL; + kfree(shm->pages); + shm->pages = NULL; +} +EXPORT_SYMBOL_GPL(tee_shm_pool_op_free_helper); + static struct tee_shm * register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags, int id) diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index 911ddf92dcee..4cf402424e71 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -275,6 +275,17 @@ void *tee_get_drvdata(struct tee_device *teedev); struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size); struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size); +int tee_shm_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, + size_t size, size_t align, + int (*shm_register)(struct tee_context *ctx, + struct tee_shm *shm, + struct page **pages, + size_t num_pages, + unsigned long start)); +void tee_shm_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm, + int (*shm_unregister)(struct tee_context *ctx, + struct tee_shm *shm)); + struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length);