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