Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2015-2017, 2019-2021 Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/anon_inodes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/tee_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "tee_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static void release_registered_pages(struct tee_shm *shm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	if (shm->pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 		if (shm->flags & TEE_SHM_USER_MAPPED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 			unpin_user_pages(shm->pages, shm->num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 			size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 			for (n = 0; n < shm->num_pages; n++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 				put_page(shm->pages[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		kfree(shm->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (shm->flags & TEE_SHM_POOL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		struct tee_shm_pool_mgr *poolm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		if (shm->flags & TEE_SHM_DMA_BUF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			poolm = teedev->pool->dma_buf_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			poolm = teedev->pool->private_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		poolm->ops->free(poolm, shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	} else if (shm->flags & TEE_SHM_REGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			dev_err(teedev->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 				"unregister shm %p failed: %d", shm, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		release_registered_pages(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	teedev_ctx_put(shm->ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	kfree(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	tee_device_put(teedev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct tee_device *teedev = ctx->teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct tee_shm_pool_mgr *poolm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct tee_shm *shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (!(flags & TEE_SHM_MAPPED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		dev_err(teedev->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			"only mapped allocations supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_PRIV))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!tee_device_get(teedev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!teedev->pool) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		/* teedev has been detached from driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		ret = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		goto err_dev_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	shm = kzalloc(sizeof(*shm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (!shm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		ret = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		goto err_dev_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	refcount_set(&shm->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	shm->flags = flags | TEE_SHM_POOL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	shm->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (flags & TEE_SHM_DMA_BUF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		poolm = teedev->pool->dma_buf_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		poolm = teedev->pool->private_mgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	rc = poolm->ops->alloc(poolm, shm, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		ret = ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		goto err_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (flags & TEE_SHM_DMA_BUF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		mutex_lock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		mutex_unlock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (shm->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			ret = ERR_PTR(shm->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			goto err_pool_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	teedev_ctx_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err_pool_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	poolm->ops->free(poolm, shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) err_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	kfree(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) err_dev_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	tee_device_put(teedev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) EXPORT_SYMBOL_GPL(tee_shm_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * @ctx:	Context that allocates the shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * @size:	Requested size of shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * The returned memory registered in secure world and is suitable to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * passed as a memory buffer in parameter argument to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * tee_client_invoke_func(). The memory allocated is later freed with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * call to tee_shm_free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * @returns a pointer to 'struct tee_shm'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				 size_t length, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct tee_device *teedev = ctx->teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct tee_shm *shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (flags != req_user_flags && flags != req_kernel_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return ERR_PTR(-ENOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (!tee_device_get(teedev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (!teedev->desc->ops->shm_register ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	    !teedev->desc->ops->shm_unregister) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		tee_device_put(teedev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return ERR_PTR(-ENOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	teedev_ctx_get(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	shm = kzalloc(sizeof(*shm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (!shm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		ret = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	refcount_set(&shm->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	shm->flags = flags | TEE_SHM_REGISTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	shm->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	shm->id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	addr = untagged_addr(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	start = rounddown(addr, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	shm->offset = addr - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	shm->size = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!shm->pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		ret = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (flags & TEE_SHM_USER_MAPPED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 					 shm->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		struct kvec *kiov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		if (!kiov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			ret = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			kiov[i].iov_base = (void *)(start + i * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			kiov[i].iov_len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		rc = get_kernel_pages(kiov, num_pages, 0, shm->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		kfree(kiov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		shm->num_pages = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (rc != num_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		ret = ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	mutex_lock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	mutex_unlock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (shm->id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ret = ERR_PTR(shm->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 					     shm->num_pages, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		ret = ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (shm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (shm->id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			mutex_lock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			idr_remove(&teedev->idr, shm->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			mutex_unlock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		release_registered_pages(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	kfree(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	teedev_ctx_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	tee_device_put(teedev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) EXPORT_SYMBOL_GPL(tee_shm_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int tee_shm_fop_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	tee_shm_put(filp->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct tee_shm *shm = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	size_t size = vma->vm_end - vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	/* Refuse sharing shared memory provided by application */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (shm->flags & TEE_SHM_USER_MAPPED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	/* check for overflowing the buffer's size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			       size, vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static const struct file_operations tee_shm_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	.release = tee_shm_fop_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	.mmap = tee_shm_fop_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * tee_shm_get_fd() - Increase reference count and return file descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * @shm:	Shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * @returns user space file descriptor to shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int tee_shm_get_fd(struct tee_shm *shm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (!(shm->flags & TEE_SHM_DMA_BUF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	/* matched by tee_shm_put() in tee_shm_op_release() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	refcount_inc(&shm->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		tee_shm_put(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * tee_shm_free() - Free shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * @shm:	Handle to shared memory to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) void tee_shm_free(struct tee_shm *shm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	tee_shm_put(shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) EXPORT_SYMBOL_GPL(tee_shm_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * tee_shm_va2pa() - Get physical address of a virtual address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * @shm:	Shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  * @va:		Virtual address to tranlsate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * @pa:		Returned physical address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * @returns 0 on success and < 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (!(shm->flags & TEE_SHM_MAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	/* Check that we're in the range of the shm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if ((char *)va < (char *)shm->kaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if ((char *)va >= ((char *)shm->kaddr + shm->size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return tee_shm_get_pa(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) EXPORT_SYMBOL_GPL(tee_shm_va2pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * tee_shm_pa2va() - Get virtual address of a physical address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * @shm:	Shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * @pa:		Physical address to tranlsate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * @va:		Returned virtual address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * @returns 0 on success and < 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (!(shm->flags & TEE_SHM_MAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	/* Check that we're in the range of the shm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (pa < shm->paddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (pa >= (shm->paddr + shm->size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (va) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		void *v = tee_shm_get_va(shm, pa - shm->paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		if (IS_ERR(v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			return PTR_ERR(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		*va = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) EXPORT_SYMBOL_GPL(tee_shm_pa2va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  * @shm:	Shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * @offs:	Offset from start of this shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  * @returns virtual address of the shared memory + offs if offs is within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  *	the bounds of this shared memory, else an ERR_PTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (!(shm->flags & TEE_SHM_MAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (offs >= shm->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	return (char *)shm->kaddr + offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) EXPORT_SYMBOL_GPL(tee_shm_get_va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * @shm:	Shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * @offs:	Offset from start of this shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * @pa:		Physical address to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * @returns 0 if offs is within the bounds of this shared memory, else an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  *	error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (offs >= shm->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		*pa = shm->paddr + offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) EXPORT_SYMBOL_GPL(tee_shm_get_pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * tee_shm_get_from_id() - Find shared memory object and increase reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  * count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * @ctx:	Context owning the shared memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * @id:		Id of shared memory object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	struct tee_device *teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	struct tee_shm *shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	teedev = ctx->teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	mutex_lock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	shm = idr_find(&teedev->idr, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	 * If the tee_shm was found in the IDR it must have a refcount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	 * larger than 0 due to the guarantee in tee_shm_put() below. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	 * it's safe to use refcount_inc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (!shm || shm->ctx != ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		shm = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		refcount_inc(&shm->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	mutex_unlock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	return shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  * tee_shm_put() - Decrease reference count on a shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  * @shm:	Shared memory handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) void tee_shm_put(struct tee_shm *shm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct tee_device *teedev = shm->ctx->teedev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	bool do_release = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	mutex_lock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (refcount_dec_and_test(&shm->refcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		 * refcount has reached 0, we must now remove it from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		 * IDR before releasing the mutex. This will guarantee that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		 * the refcount_inc() in tee_shm_get_from_id() never starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		 * from 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		if (shm->flags & TEE_SHM_DMA_BUF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			idr_remove(&teedev->idr, shm->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		do_release = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	mutex_unlock(&teedev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (do_release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		tee_shm_release(teedev, shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) EXPORT_SYMBOL_GPL(tee_shm_put);