tee_shm.c 13 KB

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