sync_file.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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_obj(*sync_file);
  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. const char __rcu *timeline;
  113. const char __rcu *driver;
  114. rcu_read_lock();
  115. driver = dma_fence_driver_name(fence);
  116. timeline = dma_fence_timeline_name(fence);
  117. snprintf(buf, len, "%s-%s%llu-%lld",
  118. rcu_dereference(driver),
  119. rcu_dereference(timeline),
  120. fence->context,
  121. fence->seqno);
  122. rcu_read_unlock();
  123. }
  124. return buf;
  125. }
  126. /**
  127. * sync_file_merge() - merge two sync_files
  128. * @name: name of new fence
  129. * @a: sync_file a
  130. * @b: sync_file b
  131. *
  132. * Creates a new sync_file which contains copies of all the fences in both
  133. * @a and @b. @a and @b remain valid, independent sync_file. Returns the
  134. * new merged sync_file or NULL in case of error.
  135. */
  136. static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
  137. struct sync_file *b)
  138. {
  139. struct sync_file *sync_file;
  140. struct dma_fence *fence;
  141. sync_file = sync_file_alloc();
  142. if (!sync_file)
  143. return NULL;
  144. fence = dma_fence_unwrap_merge(a->fence, b->fence);
  145. if (!fence) {
  146. fput(sync_file->file);
  147. return NULL;
  148. }
  149. sync_file->fence = fence;
  150. strscpy(sync_file->user_name, name, sizeof(sync_file->user_name));
  151. return sync_file;
  152. }
  153. static int sync_file_release(struct inode *inode, struct file *file)
  154. {
  155. struct sync_file *sync_file = file->private_data;
  156. if (test_bit(POLL_ENABLED, &sync_file->flags))
  157. dma_fence_remove_callback(sync_file->fence, &sync_file->cb);
  158. dma_fence_put(sync_file->fence);
  159. kfree(sync_file);
  160. return 0;
  161. }
  162. static __poll_t sync_file_poll(struct file *file, poll_table *wait)
  163. {
  164. struct sync_file *sync_file = file->private_data;
  165. poll_wait(file, &sync_file->wq, wait);
  166. if (list_empty(&sync_file->cb.node) &&
  167. !test_and_set_bit(POLL_ENABLED, &sync_file->flags)) {
  168. if (dma_fence_add_callback(sync_file->fence, &sync_file->cb,
  169. fence_check_cb_func) < 0)
  170. wake_up_all(&sync_file->wq);
  171. }
  172. return dma_fence_is_signaled(sync_file->fence) ? EPOLLIN : 0;
  173. }
  174. static long sync_file_ioctl_merge(struct sync_file *sync_file,
  175. unsigned long arg)
  176. {
  177. int fd = get_unused_fd_flags(O_CLOEXEC);
  178. int err;
  179. struct sync_file *fence2, *fence3;
  180. struct sync_merge_data data;
  181. if (fd < 0)
  182. return fd;
  183. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  184. err = -EFAULT;
  185. goto err_put_fd;
  186. }
  187. if (data.flags || data.pad) {
  188. err = -EINVAL;
  189. goto err_put_fd;
  190. }
  191. fence2 = sync_file_fdget(data.fd2);
  192. if (!fence2) {
  193. err = -ENOENT;
  194. goto err_put_fd;
  195. }
  196. data.name[sizeof(data.name) - 1] = '\0';
  197. fence3 = sync_file_merge(data.name, sync_file, fence2);
  198. if (!fence3) {
  199. err = -ENOMEM;
  200. goto err_put_fence2;
  201. }
  202. data.fence = fd;
  203. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  204. err = -EFAULT;
  205. goto err_put_fence3;
  206. }
  207. fd_install(fd, fence3->file);
  208. fput(fence2->file);
  209. return 0;
  210. err_put_fence3:
  211. fput(fence3->file);
  212. err_put_fence2:
  213. fput(fence2->file);
  214. err_put_fd:
  215. put_unused_fd(fd);
  216. return err;
  217. }
  218. static int sync_fill_fence_info(struct dma_fence *fence,
  219. struct sync_fence_info *info)
  220. {
  221. const char __rcu *timeline;
  222. const char __rcu *driver;
  223. rcu_read_lock();
  224. driver = dma_fence_driver_name(fence);
  225. timeline = dma_fence_timeline_name(fence);
  226. strscpy(info->obj_name, rcu_dereference(timeline),
  227. sizeof(info->obj_name));
  228. strscpy(info->driver_name, rcu_dereference(driver),
  229. sizeof(info->driver_name));
  230. info->status = dma_fence_get_status(fence);
  231. info->timestamp_ns =
  232. dma_fence_is_signaled(fence) ?
  233. ktime_to_ns(dma_fence_timestamp(fence)) :
  234. ktime_set(0, 0);
  235. rcu_read_unlock();
  236. return info->status;
  237. }
  238. static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  239. unsigned long arg)
  240. {
  241. struct sync_fence_info *fence_info = NULL;
  242. struct dma_fence_unwrap iter;
  243. struct sync_file_info info;
  244. unsigned int num_fences;
  245. struct dma_fence *fence;
  246. int ret;
  247. __u32 size;
  248. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  249. return -EFAULT;
  250. if (info.flags || info.pad)
  251. return -EINVAL;
  252. num_fences = 0;
  253. dma_fence_unwrap_for_each(fence, &iter, sync_file->fence)
  254. ++num_fences;
  255. /*
  256. * Passing num_fences = 0 means that userspace doesn't want to
  257. * retrieve any sync_fence_info. If num_fences = 0 we skip filling
  258. * sync_fence_info and return the actual number of fences on
  259. * info->num_fences.
  260. */
  261. if (!info.num_fences) {
  262. info.status = dma_fence_get_status(sync_file->fence);
  263. goto no_fences;
  264. } else {
  265. info.status = 1;
  266. }
  267. if (info.num_fences < num_fences)
  268. return -EINVAL;
  269. size = num_fences * sizeof(*fence_info);
  270. fence_info = kzalloc(size, GFP_KERNEL);
  271. if (!fence_info)
  272. return -ENOMEM;
  273. num_fences = 0;
  274. dma_fence_unwrap_for_each(fence, &iter, sync_file->fence) {
  275. int status;
  276. status = sync_fill_fence_info(fence, &fence_info[num_fences++]);
  277. info.status = info.status <= 0 ? info.status : status;
  278. }
  279. if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  280. size)) {
  281. ret = -EFAULT;
  282. goto out;
  283. }
  284. no_fences:
  285. sync_file_get_name(sync_file, info.name, sizeof(info.name));
  286. info.num_fences = num_fences;
  287. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  288. ret = -EFAULT;
  289. else
  290. ret = 0;
  291. out:
  292. kfree(fence_info);
  293. return ret;
  294. }
  295. static int sync_file_ioctl_set_deadline(struct sync_file *sync_file,
  296. unsigned long arg)
  297. {
  298. struct sync_set_deadline ts;
  299. if (copy_from_user(&ts, (void __user *)arg, sizeof(ts)))
  300. return -EFAULT;
  301. if (ts.pad)
  302. return -EINVAL;
  303. dma_fence_set_deadline(sync_file->fence, ns_to_ktime(ts.deadline_ns));
  304. return 0;
  305. }
  306. static long sync_file_ioctl(struct file *file, unsigned int cmd,
  307. unsigned long arg)
  308. {
  309. struct sync_file *sync_file = file->private_data;
  310. switch (cmd) {
  311. case SYNC_IOC_MERGE:
  312. return sync_file_ioctl_merge(sync_file, arg);
  313. case SYNC_IOC_FILE_INFO:
  314. return sync_file_ioctl_fence_info(sync_file, arg);
  315. case SYNC_IOC_SET_DEADLINE:
  316. return sync_file_ioctl_set_deadline(sync_file, arg);
  317. default:
  318. return -ENOTTY;
  319. }
  320. }
  321. static const struct file_operations sync_file_fops = {
  322. .release = sync_file_release,
  323. .poll = sync_file_poll,
  324. .unlocked_ioctl = sync_file_ioctl,
  325. .compat_ioctl = compat_ptr_ioctl,
  326. };