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