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