sync_file.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/dma-buf/sync_file.c
  4. *
  5. * Copyright (C) 2012 Google, Inc.
  6. */
  7. #include <linux/dma-fence-unwrap.h>
  8. #include <linux/export.h>
  9. #include <linux/file.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/poll.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/anon_inodes.h>
  17. #include <linux/sync_file.h>
  18. #include <uapi/linux/sync_file.h>
  19. static const struct file_operations sync_file_fops;
  20. static struct sync_file *sync_file_alloc(void)
  21. {
  22. struct sync_file *sync_file;
  23. sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL);
  24. if (!sync_file)
  25. return NULL;
  26. sync_file->file = anon_inode_getfile("sync_file", &sync_file_fops,
  27. sync_file, 0);
  28. if (IS_ERR(sync_file->file))
  29. goto err;
  30. init_waitqueue_head(&sync_file->wq);
  31. INIT_LIST_HEAD(&sync_file->cb.node);
  32. return sync_file;
  33. err:
  34. kfree(sync_file);
  35. return NULL;
  36. }
  37. static void fence_check_cb_func(struct dma_fence *f, struct dma_fence_cb *cb)
  38. {
  39. struct sync_file *sync_file;
  40. sync_file = container_of(cb, struct sync_file, cb);
  41. wake_up_all(&sync_file->wq);
  42. }
  43. /**
  44. * sync_file_create() - creates a sync file
  45. * @fence: fence to add to the sync_fence
  46. *
  47. * Creates a sync_file containg @fence. This function acquires and additional
  48. * reference of @fence for the newly-created &sync_file, if it succeeds. The
  49. * sync_file can be released with fput(sync_file->file). Returns the
  50. * sync_file or NULL in case of error.
  51. */
  52. struct sync_file *sync_file_create(struct dma_fence *fence)
  53. {
  54. struct sync_file *sync_file;
  55. sync_file = sync_file_alloc();
  56. if (!sync_file)
  57. return NULL;
  58. sync_file->fence = dma_fence_get(fence);
  59. return sync_file;
  60. }
  61. EXPORT_SYMBOL(sync_file_create);
  62. static struct sync_file *sync_file_fdget(int fd)
  63. {
  64. struct file *file = fget(fd);
  65. if (!file)
  66. return NULL;
  67. if (file->f_op != &sync_file_fops)
  68. goto err;
  69. return file->private_data;
  70. err:
  71. fput(file);
  72. return NULL;
  73. }
  74. /**
  75. * sync_file_get_fence - get the fence related to the sync_file fd
  76. * @fd: sync_file fd to get the fence from
  77. *
  78. * Ensures @fd references a valid sync_file and returns a fence that
  79. * represents all fence in the sync_file. On error NULL is returned.
  80. */
  81. struct dma_fence *sync_file_get_fence(int fd)
  82. {
  83. struct sync_file *sync_file;
  84. struct dma_fence *fence;
  85. sync_file = sync_file_fdget(fd);
  86. if (!sync_file)
  87. return NULL;
  88. fence = dma_fence_get(sync_file->fence);
  89. fput(sync_file->file);
  90. return fence;
  91. }
  92. EXPORT_SYMBOL(sync_file_get_fence);
  93. /**
  94. * sync_file_get_name - get the name of the sync_file
  95. * @sync_file: sync_file to get the fence from
  96. * @buf: destination buffer to copy sync_file name into
  97. * @len: available size of destination buffer.
  98. *
  99. * Each sync_file may have a name assigned either by the user (when merging
  100. * sync_files together) or created from the fence it contains. In the latter
  101. * case construction of the name is deferred until use, and so requires
  102. * sync_file_get_name().
  103. *
  104. * Returns: a string representing the name.
  105. */
  106. char *sync_file_get_name(struct sync_file *sync_file, char *buf, int len)
  107. {
  108. if (sync_file->user_name[0]) {
  109. strscpy(buf, sync_file->user_name, len);
  110. } else {
  111. struct dma_fence *fence = sync_file->fence;
  112. snprintf(buf, len, "%s-%s%llu-%lld",
  113. fence->ops->get_driver_name(fence),
  114. fence->ops->get_timeline_name(fence),
  115. fence->context,
  116. fence->seqno);
  117. }
  118. return buf;
  119. }
  120. /**
  121. * sync_file_merge() - merge two sync_files
  122. * @name: name of new fence
  123. * @a: sync_file a
  124. * @b: sync_file b
  125. *
  126. * Creates a new sync_file which contains copies of all the fences in both
  127. * @a and @b. @a and @b remain valid, independent sync_file. Returns the
  128. * new merged sync_file or NULL in case of error.
  129. */
  130. static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
  131. struct sync_file *b)
  132. {
  133. struct sync_file *sync_file;
  134. struct dma_fence *fence;
  135. sync_file = sync_file_alloc();
  136. if (!sync_file)
  137. return NULL;
  138. fence = dma_fence_unwrap_merge(a->fence, b->fence);
  139. if (!fence) {
  140. fput(sync_file->file);
  141. return NULL;
  142. }
  143. sync_file->fence = fence;
  144. strscpy(sync_file->user_name, name, sizeof(sync_file->user_name));
  145. return sync_file;
  146. }
  147. static int sync_file_release(struct inode *inode, struct file *file)
  148. {
  149. struct sync_file *sync_file = file->private_data;
  150. if (test_bit(POLL_ENABLED, &sync_file->flags))
  151. dma_fence_remove_callback(sync_file->fence, &sync_file->cb);
  152. dma_fence_put(sync_file->fence);
  153. kfree(sync_file);
  154. return 0;
  155. }
  156. static __poll_t sync_file_poll(struct file *file, poll_table *wait)
  157. {
  158. struct sync_file *sync_file = file->private_data;
  159. poll_wait(file, &sync_file->wq, wait);
  160. if (list_empty(&sync_file->cb.node) &&
  161. !test_and_set_bit(POLL_ENABLED, &sync_file->flags)) {
  162. if (dma_fence_add_callback(sync_file->fence, &sync_file->cb,
  163. fence_check_cb_func) < 0)
  164. wake_up_all(&sync_file->wq);
  165. }
  166. return dma_fence_is_signaled(sync_file->fence) ? EPOLLIN : 0;
  167. }
  168. static long sync_file_ioctl_merge(struct sync_file *sync_file,
  169. unsigned long arg)
  170. {
  171. int fd = get_unused_fd_flags(O_CLOEXEC);
  172. int err;
  173. struct sync_file *fence2, *fence3;
  174. struct sync_merge_data data;
  175. if (fd < 0)
  176. return fd;
  177. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  178. err = -EFAULT;
  179. goto err_put_fd;
  180. }
  181. if (data.flags || data.pad) {
  182. err = -EINVAL;
  183. goto err_put_fd;
  184. }
  185. fence2 = sync_file_fdget(data.fd2);
  186. if (!fence2) {
  187. err = -ENOENT;
  188. goto err_put_fd;
  189. }
  190. data.name[sizeof(data.name) - 1] = '\0';
  191. fence3 = sync_file_merge(data.name, sync_file, fence2);
  192. if (!fence3) {
  193. err = -ENOMEM;
  194. goto err_put_fence2;
  195. }
  196. data.fence = fd;
  197. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  198. err = -EFAULT;
  199. goto err_put_fence3;
  200. }
  201. fd_install(fd, fence3->file);
  202. fput(fence2->file);
  203. return 0;
  204. err_put_fence3:
  205. fput(fence3->file);
  206. err_put_fence2:
  207. fput(fence2->file);
  208. err_put_fd:
  209. put_unused_fd(fd);
  210. return err;
  211. }
  212. static int sync_fill_fence_info(struct dma_fence *fence,
  213. struct sync_fence_info *info)
  214. {
  215. strscpy(info->obj_name, fence->ops->get_timeline_name(fence),
  216. sizeof(info->obj_name));
  217. strscpy(info->driver_name, fence->ops->get_driver_name(fence),
  218. sizeof(info->driver_name));
  219. info->status = dma_fence_get_status(fence);
  220. info->timestamp_ns =
  221. dma_fence_is_signaled(fence) ?
  222. ktime_to_ns(dma_fence_timestamp(fence)) :
  223. ktime_set(0, 0);
  224. return info->status;
  225. }
  226. static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  227. unsigned long arg)
  228. {
  229. struct sync_fence_info *fence_info = NULL;
  230. struct dma_fence_unwrap iter;
  231. struct sync_file_info info;
  232. unsigned int num_fences;
  233. struct dma_fence *fence;
  234. int ret;
  235. __u32 size;
  236. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  237. return -EFAULT;
  238. if (info.flags || info.pad)
  239. return -EINVAL;
  240. num_fences = 0;
  241. dma_fence_unwrap_for_each(fence, &iter, sync_file->fence)
  242. ++num_fences;
  243. /*
  244. * Passing num_fences = 0 means that userspace doesn't want to
  245. * retrieve any sync_fence_info. If num_fences = 0 we skip filling
  246. * sync_fence_info and return the actual number of fences on
  247. * info->num_fences.
  248. */
  249. if (!info.num_fences) {
  250. info.status = dma_fence_get_status(sync_file->fence);
  251. goto no_fences;
  252. } else {
  253. info.status = 1;
  254. }
  255. if (info.num_fences < num_fences)
  256. return -EINVAL;
  257. size = num_fences * sizeof(*fence_info);
  258. fence_info = kzalloc(size, GFP_KERNEL);
  259. if (!fence_info)
  260. return -ENOMEM;
  261. num_fences = 0;
  262. dma_fence_unwrap_for_each(fence, &iter, sync_file->fence) {
  263. int status;
  264. status = sync_fill_fence_info(fence, &fence_info[num_fences++]);
  265. info.status = info.status <= 0 ? info.status : status;
  266. }
  267. if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  268. size)) {
  269. ret = -EFAULT;
  270. goto out;
  271. }
  272. no_fences:
  273. sync_file_get_name(sync_file, info.name, sizeof(info.name));
  274. info.num_fences = num_fences;
  275. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  276. ret = -EFAULT;
  277. else
  278. ret = 0;
  279. out:
  280. kfree(fence_info);
  281. return ret;
  282. }
  283. static long sync_file_ioctl(struct file *file, unsigned int cmd,
  284. unsigned long arg)
  285. {
  286. struct sync_file *sync_file = file->private_data;
  287. switch (cmd) {
  288. case SYNC_IOC_MERGE:
  289. return sync_file_ioctl_merge(sync_file, arg);
  290. case SYNC_IOC_FILE_INFO:
  291. return sync_file_ioctl_fence_info(sync_file, arg);
  292. default:
  293. return -ENOTTY;
  294. }
  295. }
  296. static const struct file_operations sync_file_fops = {
  297. .release = sync_file_release,
  298. .poll = sync_file_poll,
  299. .unlocked_ioctl = sync_file_ioctl,
  300. .compat_ioctl = compat_ptr_ioctl,
  301. };