tee_shm.c 14 KB

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