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