sync_file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * drivers/dma-buf/sync_file.c
  3. *
  4. * Copyright (C) 2012 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/export.h>
  17. #include <linux/file.h>
  18. #include <linux/fs.h>
  19. #include <linux/kernel.h>
  20. #include <linux/poll.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/anon_inodes.h>
  25. #include <linux/sync_file.h>
  26. #include <uapi/linux/sync_file.h>
  27. static const struct file_operations sync_file_fops;
  28. static struct sync_file *sync_file_alloc(void)
  29. {
  30. struct sync_file *sync_file;
  31. sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL);
  32. if (!sync_file)
  33. return NULL;
  34. sync_file->file = anon_inode_getfile("sync_file", &sync_file_fops,
  35. sync_file, 0);
  36. if (IS_ERR(sync_file->file))
  37. goto err;
  38. kref_init(&sync_file->kref);
  39. init_waitqueue_head(&sync_file->wq);
  40. INIT_LIST_HEAD(&sync_file->cb.node);
  41. return sync_file;
  42. err:
  43. kfree(sync_file);
  44. return NULL;
  45. }
  46. static void fence_check_cb_func(struct dma_fence *f, struct dma_fence_cb *cb)
  47. {
  48. struct sync_file *sync_file;
  49. sync_file = container_of(cb, struct sync_file, cb);
  50. wake_up_all(&sync_file->wq);
  51. }
  52. /**
  53. * sync_file_create() - creates a sync file
  54. * @fence: fence to add to the sync_fence
  55. *
  56. * Creates a sync_file containg @fence. This function acquires and additional
  57. * reference of @fence for the newly-created &sync_file, if it succeeds. The
  58. * sync_file can be released with fput(sync_file->file). Returns the
  59. * sync_file or NULL in case of error.
  60. */
  61. struct sync_file *sync_file_create(struct dma_fence *fence)
  62. {
  63. struct sync_file *sync_file;
  64. sync_file = sync_file_alloc();
  65. if (!sync_file)
  66. return NULL;
  67. sync_file->fence = dma_fence_get(fence);
  68. snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d",
  69. fence->ops->get_driver_name(fence),
  70. fence->ops->get_timeline_name(fence), fence->context,
  71. fence->seqno);
  72. return sync_file;
  73. }
  74. EXPORT_SYMBOL(sync_file_create);
  75. static struct sync_file *sync_file_fdget(int fd)
  76. {
  77. struct file *file = fget(fd);
  78. if (!file)
  79. return NULL;
  80. if (file->f_op != &sync_file_fops)
  81. goto err;
  82. return file->private_data;
  83. err:
  84. fput(file);
  85. return NULL;
  86. }
  87. /**
  88. * sync_file_get_fence - get the fence related to the sync_file fd
  89. * @fd: sync_file fd to get the fence from
  90. *
  91. * Ensures @fd references a valid sync_file and returns a fence that
  92. * represents all fence in the sync_file. On error NULL is returned.
  93. */
  94. struct dma_fence *sync_file_get_fence(int fd)
  95. {
  96. struct sync_file *sync_file;
  97. struct dma_fence *fence;
  98. sync_file = sync_file_fdget(fd);
  99. if (!sync_file)
  100. return NULL;
  101. fence = dma_fence_get(sync_file->fence);
  102. fput(sync_file->file);
  103. return fence;
  104. }
  105. EXPORT_SYMBOL(sync_file_get_fence);
  106. static int sync_file_set_fence(struct sync_file *sync_file,
  107. struct dma_fence **fences, int num_fences)
  108. {
  109. struct dma_fence_array *array;
  110. /*
  111. * The reference for the fences in the new sync_file and held
  112. * in add_fence() during the merge procedure, so for num_fences == 1
  113. * we already own a new reference to the fence. For num_fence > 1
  114. * we own the reference of the dma_fence_array creation.
  115. */
  116. if (num_fences == 1) {
  117. sync_file->fence = fences[0];
  118. kfree(fences);
  119. } else {
  120. array = dma_fence_array_create(num_fences, fences,
  121. dma_fence_context_alloc(1),
  122. 1, false);
  123. if (!array)
  124. return -ENOMEM;
  125. sync_file->fence = &array->base;
  126. }
  127. return 0;
  128. }
  129. static struct dma_fence **get_fences(struct sync_file *sync_file,
  130. int *num_fences)
  131. {
  132. if (dma_fence_is_array(sync_file->fence)) {
  133. struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
  134. *num_fences = array->num_fences;
  135. return array->fences;
  136. }
  137. *num_fences = 1;
  138. return &sync_file->fence;
  139. }
  140. static void add_fence(struct dma_fence **fences,
  141. int *i, struct dma_fence *fence)
  142. {
  143. fences[*i] = fence;
  144. if (!dma_fence_is_signaled(fence)) {
  145. dma_fence_get(fence);
  146. (*i)++;
  147. }
  148. }
  149. /**
  150. * sync_file_merge() - merge two sync_files
  151. * @name: name of new fence
  152. * @a: sync_file a
  153. * @b: sync_file b
  154. *
  155. * Creates a new sync_file which contains copies of all the fences in both
  156. * @a and @b. @a and @b remain valid, independent sync_file. Returns the
  157. * new merged sync_file or NULL in case of error.
  158. */
  159. static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
  160. struct sync_file *b)
  161. {
  162. struct sync_file *sync_file;
  163. struct dma_fence **fences, **nfences, **a_fences, **b_fences;
  164. int i, i_a, i_b, num_fences, a_num_fences, b_num_fences;
  165. sync_file = sync_file_alloc();
  166. if (!sync_file)
  167. return NULL;
  168. a_fences = get_fences(a, &a_num_fences);
  169. b_fences = get_fences(b, &b_num_fences);
  170. if (a_num_fences > INT_MAX - b_num_fences)
  171. return NULL;
  172. num_fences = a_num_fences + b_num_fences;
  173. fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
  174. if (!fences)
  175. goto err;
  176. /*
  177. * Assume sync_file a and b are both ordered and have no
  178. * duplicates with the same context.
  179. *
  180. * If a sync_file can only be created with sync_file_merge
  181. * and sync_file_create, this is a reasonable assumption.
  182. */
  183. for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) {
  184. struct dma_fence *pt_a = a_fences[i_a];
  185. struct dma_fence *pt_b = b_fences[i_b];
  186. if (pt_a->context < pt_b->context) {
  187. add_fence(fences, &i, pt_a);
  188. i_a++;
  189. } else if (pt_a->context > pt_b->context) {
  190. add_fence(fences, &i, pt_b);
  191. i_b++;
  192. } else {
  193. if (pt_a->seqno - pt_b->seqno <= INT_MAX)
  194. add_fence(fences, &i, pt_a);
  195. else
  196. add_fence(fences, &i, pt_b);
  197. i_a++;
  198. i_b++;
  199. }
  200. }
  201. for (; i_a < a_num_fences; i_a++)
  202. add_fence(fences, &i, a_fences[i_a]);
  203. for (; i_b < b_num_fences; i_b++)
  204. add_fence(fences, &i, b_fences[i_b]);
  205. if (i == 0)
  206. fences[i++] = dma_fence_get(a_fences[0]);
  207. if (num_fences > i) {
  208. nfences = krealloc(fences, i * sizeof(*fences),
  209. GFP_KERNEL);
  210. if (!nfences)
  211. goto err;
  212. fences = nfences;
  213. }
  214. if (sync_file_set_fence(sync_file, fences, i) < 0) {
  215. kfree(fences);
  216. goto err;
  217. }
  218. strlcpy(sync_file->name, name, sizeof(sync_file->name));
  219. return sync_file;
  220. err:
  221. fput(sync_file->file);
  222. return NULL;
  223. }
  224. static void sync_file_free(struct kref *kref)
  225. {
  226. struct sync_file *sync_file = container_of(kref, struct sync_file,
  227. kref);
  228. if (test_bit(POLL_ENABLED, &sync_file->fence->flags))
  229. dma_fence_remove_callback(sync_file->fence, &sync_file->cb);
  230. dma_fence_put(sync_file->fence);
  231. kfree(sync_file);
  232. }
  233. static int sync_file_release(struct inode *inode, struct file *file)
  234. {
  235. struct sync_file *sync_file = file->private_data;
  236. kref_put(&sync_file->kref, sync_file_free);
  237. return 0;
  238. }
  239. static unsigned int sync_file_poll(struct file *file, poll_table *wait)
  240. {
  241. struct sync_file *sync_file = file->private_data;
  242. poll_wait(file, &sync_file->wq, wait);
  243. if (!test_and_set_bit(POLL_ENABLED, &sync_file->fence->flags)) {
  244. if (dma_fence_add_callback(sync_file->fence, &sync_file->cb,
  245. fence_check_cb_func) < 0)
  246. wake_up_all(&sync_file->wq);
  247. }
  248. return dma_fence_is_signaled(sync_file->fence) ? POLLIN : 0;
  249. }
  250. static long sync_file_ioctl_merge(struct sync_file *sync_file,
  251. unsigned long arg)
  252. {
  253. int fd = get_unused_fd_flags(O_CLOEXEC);
  254. int err;
  255. struct sync_file *fence2, *fence3;
  256. struct sync_merge_data data;
  257. if (fd < 0)
  258. return fd;
  259. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  260. err = -EFAULT;
  261. goto err_put_fd;
  262. }
  263. if (data.flags || data.pad) {
  264. err = -EINVAL;
  265. goto err_put_fd;
  266. }
  267. fence2 = sync_file_fdget(data.fd2);
  268. if (!fence2) {
  269. err = -ENOENT;
  270. goto err_put_fd;
  271. }
  272. data.name[sizeof(data.name) - 1] = '\0';
  273. fence3 = sync_file_merge(data.name, sync_file, fence2);
  274. if (!fence3) {
  275. err = -ENOMEM;
  276. goto err_put_fence2;
  277. }
  278. data.fence = fd;
  279. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  280. err = -EFAULT;
  281. goto err_put_fence3;
  282. }
  283. fd_install(fd, fence3->file);
  284. fput(fence2->file);
  285. return 0;
  286. err_put_fence3:
  287. fput(fence3->file);
  288. err_put_fence2:
  289. fput(fence2->file);
  290. err_put_fd:
  291. put_unused_fd(fd);
  292. return err;
  293. }
  294. static void sync_fill_fence_info(struct dma_fence *fence,
  295. struct sync_fence_info *info)
  296. {
  297. strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
  298. sizeof(info->obj_name));
  299. strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
  300. sizeof(info->driver_name));
  301. info->status = dma_fence_get_status(fence);
  302. info->timestamp_ns = ktime_to_ns(fence->timestamp);
  303. }
  304. static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  305. unsigned long arg)
  306. {
  307. struct sync_file_info info;
  308. struct sync_fence_info *fence_info = NULL;
  309. struct dma_fence **fences;
  310. __u32 size;
  311. int num_fences, ret, i;
  312. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  313. return -EFAULT;
  314. if (info.flags || info.pad)
  315. return -EINVAL;
  316. fences = get_fences(sync_file, &num_fences);
  317. /*
  318. * Passing num_fences = 0 means that userspace doesn't want to
  319. * retrieve any sync_fence_info. If num_fences = 0 we skip filling
  320. * sync_fence_info and return the actual number of fences on
  321. * info->num_fences.
  322. */
  323. if (!info.num_fences)
  324. goto no_fences;
  325. if (info.num_fences < num_fences)
  326. return -EINVAL;
  327. size = num_fences * sizeof(*fence_info);
  328. fence_info = kzalloc(size, GFP_KERNEL);
  329. if (!fence_info)
  330. return -ENOMEM;
  331. for (i = 0; i < num_fences; i++)
  332. sync_fill_fence_info(fences[i], &fence_info[i]);
  333. if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  334. size)) {
  335. ret = -EFAULT;
  336. goto out;
  337. }
  338. no_fences:
  339. strlcpy(info.name, sync_file->name, sizeof(info.name));
  340. info.status = dma_fence_is_signaled(sync_file->fence);
  341. info.num_fences = num_fences;
  342. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  343. ret = -EFAULT;
  344. else
  345. ret = 0;
  346. out:
  347. kfree(fence_info);
  348. return ret;
  349. }
  350. static long sync_file_ioctl(struct file *file, unsigned int cmd,
  351. unsigned long arg)
  352. {
  353. struct sync_file *sync_file = file->private_data;
  354. switch (cmd) {
  355. case SYNC_IOC_MERGE:
  356. return sync_file_ioctl_merge(sync_file, arg);
  357. case SYNC_IOC_FILE_INFO:
  358. return sync_file_ioctl_fence_info(sync_file, arg);
  359. default:
  360. return -ENOTTY;
  361. }
  362. }
  363. static const struct file_operations sync_file_fops = {
  364. .release = sync_file_release,
  365. .poll = sync_file_poll,
  366. .unlocked_ioctl = sync_file_ioctl,
  367. .compat_ioctl = sync_file_ioctl,
  368. };