sync_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. init_waitqueue_head(&sync_file->wq);
  39. INIT_LIST_HEAD(&sync_file->cb.node);
  40. return sync_file;
  41. err:
  42. kfree(sync_file);
  43. return NULL;
  44. }
  45. static void fence_check_cb_func(struct dma_fence *f, struct dma_fence_cb *cb)
  46. {
  47. struct sync_file *sync_file;
  48. sync_file = container_of(cb, struct sync_file, cb);
  49. wake_up_all(&sync_file->wq);
  50. }
  51. /**
  52. * sync_file_create() - creates a sync file
  53. * @fence: fence to add to the sync_fence
  54. *
  55. * Creates a sync_file containg @fence. This function acquires and additional
  56. * reference of @fence for the newly-created &sync_file, if it succeeds. The
  57. * sync_file can be released with fput(sync_file->file). Returns the
  58. * sync_file or NULL in case of error.
  59. */
  60. struct sync_file *sync_file_create(struct dma_fence *fence)
  61. {
  62. struct sync_file *sync_file;
  63. sync_file = sync_file_alloc();
  64. if (!sync_file)
  65. return NULL;
  66. sync_file->fence = dma_fence_get(fence);
  67. return sync_file;
  68. }
  69. EXPORT_SYMBOL(sync_file_create);
  70. static struct sync_file *sync_file_fdget(int fd)
  71. {
  72. struct file *file = fget(fd);
  73. if (!file)
  74. return NULL;
  75. if (file->f_op != &sync_file_fops)
  76. goto err;
  77. return file->private_data;
  78. err:
  79. fput(file);
  80. return NULL;
  81. }
  82. /**
  83. * sync_file_get_fence - get the fence related to the sync_file fd
  84. * @fd: sync_file fd to get the fence from
  85. *
  86. * Ensures @fd references a valid sync_file and returns a fence that
  87. * represents all fence in the sync_file. On error NULL is returned.
  88. */
  89. struct dma_fence *sync_file_get_fence(int fd)
  90. {
  91. struct sync_file *sync_file;
  92. struct dma_fence *fence;
  93. sync_file = sync_file_fdget(fd);
  94. if (!sync_file)
  95. return NULL;
  96. fence = dma_fence_get(sync_file->fence);
  97. fput(sync_file->file);
  98. return fence;
  99. }
  100. EXPORT_SYMBOL(sync_file_get_fence);
  101. /**
  102. * sync_file_get_name - get the name of the sync_file
  103. * @sync_file: sync_file to get the fence from
  104. * @buf: destination buffer to copy sync_file name into
  105. * @len: available size of destination buffer.
  106. *
  107. * Each sync_file may have a name assigned either by the user (when merging
  108. * sync_files together) or created from the fence it contains. In the latter
  109. * case construction of the name is deferred until use, and so requires
  110. * sync_file_get_name().
  111. *
  112. * Returns: a string representing the name.
  113. */
  114. char *sync_file_get_name(struct sync_file *sync_file, char *buf, int len)
  115. {
  116. if (sync_file->user_name[0]) {
  117. strlcpy(buf, sync_file->user_name, len);
  118. } else {
  119. struct dma_fence *fence = sync_file->fence;
  120. snprintf(buf, len, "%s-%s%llu-%d",
  121. fence->ops->get_driver_name(fence),
  122. fence->ops->get_timeline_name(fence),
  123. fence->context,
  124. fence->seqno);
  125. }
  126. return buf;
  127. }
  128. static int sync_file_set_fence(struct sync_file *sync_file,
  129. struct dma_fence **fences, int num_fences)
  130. {
  131. struct dma_fence_array *array;
  132. /*
  133. * The reference for the fences in the new sync_file and held
  134. * in add_fence() during the merge procedure, so for num_fences == 1
  135. * we already own a new reference to the fence. For num_fence > 1
  136. * we own the reference of the dma_fence_array creation.
  137. */
  138. if (num_fences == 1) {
  139. sync_file->fence = fences[0];
  140. kfree(fences);
  141. } else {
  142. array = dma_fence_array_create(num_fences, fences,
  143. dma_fence_context_alloc(1),
  144. 1, false);
  145. if (!array)
  146. return -ENOMEM;
  147. sync_file->fence = &array->base;
  148. }
  149. return 0;
  150. }
  151. static struct dma_fence **get_fences(struct sync_file *sync_file,
  152. int *num_fences)
  153. {
  154. if (dma_fence_is_array(sync_file->fence)) {
  155. struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
  156. *num_fences = array->num_fences;
  157. return array->fences;
  158. }
  159. *num_fences = 1;
  160. return &sync_file->fence;
  161. }
  162. static void add_fence(struct dma_fence **fences,
  163. int *i, struct dma_fence *fence)
  164. {
  165. fences[*i] = fence;
  166. if (!dma_fence_is_signaled(fence)) {
  167. dma_fence_get(fence);
  168. (*i)++;
  169. }
  170. }
  171. /**
  172. * sync_file_merge() - merge two sync_files
  173. * @name: name of new fence
  174. * @a: sync_file a
  175. * @b: sync_file b
  176. *
  177. * Creates a new sync_file which contains copies of all the fences in both
  178. * @a and @b. @a and @b remain valid, independent sync_file. Returns the
  179. * new merged sync_file or NULL in case of error.
  180. */
  181. static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
  182. struct sync_file *b)
  183. {
  184. struct sync_file *sync_file;
  185. struct dma_fence **fences, **nfences, **a_fences, **b_fences;
  186. int i, i_a, i_b, num_fences, a_num_fences, b_num_fences;
  187. sync_file = sync_file_alloc();
  188. if (!sync_file)
  189. return NULL;
  190. a_fences = get_fences(a, &a_num_fences);
  191. b_fences = get_fences(b, &b_num_fences);
  192. if (a_num_fences > INT_MAX - b_num_fences)
  193. goto err;
  194. num_fences = a_num_fences + b_num_fences;
  195. fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
  196. if (!fences)
  197. goto err;
  198. /*
  199. * Assume sync_file a and b are both ordered and have no
  200. * duplicates with the same context.
  201. *
  202. * If a sync_file can only be created with sync_file_merge
  203. * and sync_file_create, this is a reasonable assumption.
  204. */
  205. for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) {
  206. struct dma_fence *pt_a = a_fences[i_a];
  207. struct dma_fence *pt_b = b_fences[i_b];
  208. if (pt_a->context < pt_b->context) {
  209. add_fence(fences, &i, pt_a);
  210. i_a++;
  211. } else if (pt_a->context > pt_b->context) {
  212. add_fence(fences, &i, pt_b);
  213. i_b++;
  214. } else {
  215. if (pt_a->seqno - pt_b->seqno <= INT_MAX)
  216. add_fence(fences, &i, pt_a);
  217. else
  218. add_fence(fences, &i, pt_b);
  219. i_a++;
  220. i_b++;
  221. }
  222. }
  223. for (; i_a < a_num_fences; i_a++)
  224. add_fence(fences, &i, a_fences[i_a]);
  225. for (; i_b < b_num_fences; i_b++)
  226. add_fence(fences, &i, b_fences[i_b]);
  227. if (i == 0)
  228. fences[i++] = dma_fence_get(a_fences[0]);
  229. if (num_fences > i) {
  230. nfences = krealloc(fences, i * sizeof(*fences),
  231. GFP_KERNEL);
  232. if (!nfences)
  233. goto err;
  234. fences = nfences;
  235. }
  236. if (sync_file_set_fence(sync_file, fences, i) < 0) {
  237. kfree(fences);
  238. goto err;
  239. }
  240. strlcpy(sync_file->user_name, name, sizeof(sync_file->user_name));
  241. return sync_file;
  242. err:
  243. fput(sync_file->file);
  244. return NULL;
  245. }
  246. static int sync_file_release(struct inode *inode, struct file *file)
  247. {
  248. struct sync_file *sync_file = file->private_data;
  249. if (test_bit(POLL_ENABLED, &sync_file->flags))
  250. dma_fence_remove_callback(sync_file->fence, &sync_file->cb);
  251. dma_fence_put(sync_file->fence);
  252. kfree(sync_file);
  253. return 0;
  254. }
  255. static __poll_t sync_file_poll(struct file *file, poll_table *wait)
  256. {
  257. struct sync_file *sync_file = file->private_data;
  258. poll_wait(file, &sync_file->wq, wait);
  259. if (list_empty(&sync_file->cb.node) &&
  260. !test_and_set_bit(POLL_ENABLED, &sync_file->flags)) {
  261. if (dma_fence_add_callback(sync_file->fence, &sync_file->cb,
  262. fence_check_cb_func) < 0)
  263. wake_up_all(&sync_file->wq);
  264. }
  265. return dma_fence_is_signaled(sync_file->fence) ? EPOLLIN : 0;
  266. }
  267. static long sync_file_ioctl_merge(struct sync_file *sync_file,
  268. unsigned long arg)
  269. {
  270. int fd = get_unused_fd_flags(O_CLOEXEC);
  271. int err;
  272. struct sync_file *fence2, *fence3;
  273. struct sync_merge_data data;
  274. if (fd < 0)
  275. return fd;
  276. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  277. err = -EFAULT;
  278. goto err_put_fd;
  279. }
  280. if (data.flags || data.pad) {
  281. err = -EINVAL;
  282. goto err_put_fd;
  283. }
  284. fence2 = sync_file_fdget(data.fd2);
  285. if (!fence2) {
  286. err = -ENOENT;
  287. goto err_put_fd;
  288. }
  289. data.name[sizeof(data.name) - 1] = '\0';
  290. fence3 = sync_file_merge(data.name, sync_file, fence2);
  291. if (!fence3) {
  292. err = -ENOMEM;
  293. goto err_put_fence2;
  294. }
  295. data.fence = fd;
  296. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  297. err = -EFAULT;
  298. goto err_put_fence3;
  299. }
  300. fd_install(fd, fence3->file);
  301. fput(fence2->file);
  302. return 0;
  303. err_put_fence3:
  304. fput(fence3->file);
  305. err_put_fence2:
  306. fput(fence2->file);
  307. err_put_fd:
  308. put_unused_fd(fd);
  309. return err;
  310. }
  311. static int sync_fill_fence_info(struct dma_fence *fence,
  312. struct sync_fence_info *info)
  313. {
  314. strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
  315. sizeof(info->obj_name));
  316. strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
  317. sizeof(info->driver_name));
  318. info->status = dma_fence_get_status(fence);
  319. while (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) &&
  320. !test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
  321. cpu_relax();
  322. info->timestamp_ns =
  323. test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags) ?
  324. ktime_to_ns(fence->timestamp) :
  325. ktime_set(0, 0);
  326. return info->status;
  327. }
  328. static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  329. unsigned long arg)
  330. {
  331. struct sync_file_info info;
  332. struct sync_fence_info *fence_info = NULL;
  333. struct dma_fence **fences;
  334. __u32 size;
  335. int num_fences, ret, i;
  336. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  337. return -EFAULT;
  338. if (info.flags || info.pad)
  339. return -EINVAL;
  340. fences = get_fences(sync_file, &num_fences);
  341. /*
  342. * Passing num_fences = 0 means that userspace doesn't want to
  343. * retrieve any sync_fence_info. If num_fences = 0 we skip filling
  344. * sync_fence_info and return the actual number of fences on
  345. * info->num_fences.
  346. */
  347. if (!info.num_fences) {
  348. info.status = dma_fence_is_signaled(sync_file->fence);
  349. goto no_fences;
  350. } else {
  351. info.status = 1;
  352. }
  353. if (info.num_fences < num_fences)
  354. return -EINVAL;
  355. size = num_fences * sizeof(*fence_info);
  356. fence_info = kzalloc(size, GFP_KERNEL);
  357. if (!fence_info)
  358. return -ENOMEM;
  359. for (i = 0; i < num_fences; i++) {
  360. int status = sync_fill_fence_info(fences[i], &fence_info[i]);
  361. info.status = info.status <= 0 ? info.status : status;
  362. }
  363. if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  364. size)) {
  365. ret = -EFAULT;
  366. goto out;
  367. }
  368. no_fences:
  369. sync_file_get_name(sync_file, info.name, sizeof(info.name));
  370. info.num_fences = num_fences;
  371. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  372. ret = -EFAULT;
  373. else
  374. ret = 0;
  375. out:
  376. kfree(fence_info);
  377. return ret;
  378. }
  379. static long sync_file_ioctl(struct file *file, unsigned int cmd,
  380. unsigned long arg)
  381. {
  382. struct sync_file *sync_file = file->private_data;
  383. switch (cmd) {
  384. case SYNC_IOC_MERGE:
  385. return sync_file_ioctl_merge(sync_file, arg);
  386. case SYNC_IOC_FILE_INFO:
  387. return sync_file_ioctl_fence_info(sync_file, arg);
  388. default:
  389. return -ENOTTY;
  390. }
  391. }
  392. static const struct file_operations sync_file_fops = {
  393. .release = sync_file_release,
  394. .poll = sync_file_poll,
  395. .unlocked_ioctl = sync_file_ioctl,
  396. .compat_ioctl = sync_file_ioctl,
  397. };