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 fence *f, struct 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 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 = 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 fence *sync_file_get_fence(int fd)
  95. {
  96. struct sync_file *sync_file;
  97. struct fence *fence;
  98. sync_file = sync_file_fdget(fd);
  99. if (!sync_file)
  100. return NULL;
  101. fence = 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 fence **fences, int num_fences)
  108. {
  109. struct 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 fence_array creation.
  115. */
  116. if (num_fences == 1) {
  117. sync_file->fence = fences[0];
  118. kfree(fences);
  119. } else {
  120. array = fence_array_create(num_fences, fences,
  121. fence_context_alloc(1), 1, false);
  122. if (!array)
  123. return -ENOMEM;
  124. sync_file->fence = &array->base;
  125. }
  126. return 0;
  127. }
  128. static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)
  129. {
  130. if (fence_is_array(sync_file->fence)) {
  131. struct fence_array *array = to_fence_array(sync_file->fence);
  132. *num_fences = array->num_fences;
  133. return array->fences;
  134. }
  135. *num_fences = 1;
  136. return &sync_file->fence;
  137. }
  138. static void add_fence(struct fence **fences, int *i, struct fence *fence)
  139. {
  140. fences[*i] = fence;
  141. if (!fence_is_signaled(fence)) {
  142. fence_get(fence);
  143. (*i)++;
  144. }
  145. }
  146. /**
  147. * sync_file_merge() - merge two sync_files
  148. * @name: name of new fence
  149. * @a: sync_file a
  150. * @b: sync_file b
  151. *
  152. * Creates a new sync_file which contains copies of all the fences in both
  153. * @a and @b. @a and @b remain valid, independent sync_file. Returns the
  154. * new merged sync_file or NULL in case of error.
  155. */
  156. static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
  157. struct sync_file *b)
  158. {
  159. struct sync_file *sync_file;
  160. struct fence **fences, **nfences, **a_fences, **b_fences;
  161. int i, i_a, i_b, num_fences, a_num_fences, b_num_fences;
  162. sync_file = sync_file_alloc();
  163. if (!sync_file)
  164. return NULL;
  165. a_fences = get_fences(a, &a_num_fences);
  166. b_fences = get_fences(b, &b_num_fences);
  167. if (a_num_fences > INT_MAX - b_num_fences)
  168. return NULL;
  169. num_fences = a_num_fences + b_num_fences;
  170. fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
  171. if (!fences)
  172. goto err;
  173. /*
  174. * Assume sync_file a and b are both ordered and have no
  175. * duplicates with the same context.
  176. *
  177. * If a sync_file can only be created with sync_file_merge
  178. * and sync_file_create, this is a reasonable assumption.
  179. */
  180. for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) {
  181. struct fence *pt_a = a_fences[i_a];
  182. struct fence *pt_b = b_fences[i_b];
  183. if (pt_a->context < pt_b->context) {
  184. add_fence(fences, &i, pt_a);
  185. i_a++;
  186. } else if (pt_a->context > pt_b->context) {
  187. add_fence(fences, &i, pt_b);
  188. i_b++;
  189. } else {
  190. if (pt_a->seqno - pt_b->seqno <= INT_MAX)
  191. add_fence(fences, &i, pt_a);
  192. else
  193. add_fence(fences, &i, pt_b);
  194. i_a++;
  195. i_b++;
  196. }
  197. }
  198. for (; i_a < a_num_fences; i_a++)
  199. add_fence(fences, &i, a_fences[i_a]);
  200. for (; i_b < b_num_fences; i_b++)
  201. add_fence(fences, &i, b_fences[i_b]);
  202. if (i == 0)
  203. fences[i++] = fence_get(a_fences[0]);
  204. if (num_fences > i) {
  205. nfences = krealloc(fences, i * sizeof(*fences),
  206. GFP_KERNEL);
  207. if (!nfences)
  208. goto err;
  209. fences = nfences;
  210. }
  211. if (sync_file_set_fence(sync_file, fences, i) < 0) {
  212. kfree(fences);
  213. goto err;
  214. }
  215. strlcpy(sync_file->name, name, sizeof(sync_file->name));
  216. return sync_file;
  217. err:
  218. fput(sync_file->file);
  219. return NULL;
  220. }
  221. static void sync_file_free(struct kref *kref)
  222. {
  223. struct sync_file *sync_file = container_of(kref, struct sync_file,
  224. kref);
  225. if (test_bit(POLL_ENABLED, &sync_file->fence->flags))
  226. fence_remove_callback(sync_file->fence, &sync_file->cb);
  227. fence_put(sync_file->fence);
  228. kfree(sync_file);
  229. }
  230. static int sync_file_release(struct inode *inode, struct file *file)
  231. {
  232. struct sync_file *sync_file = file->private_data;
  233. kref_put(&sync_file->kref, sync_file_free);
  234. return 0;
  235. }
  236. static unsigned int sync_file_poll(struct file *file, poll_table *wait)
  237. {
  238. struct sync_file *sync_file = file->private_data;
  239. poll_wait(file, &sync_file->wq, wait);
  240. if (!poll_does_not_wait(wait) &&
  241. !test_and_set_bit(POLL_ENABLED, &sync_file->fence->flags)) {
  242. if (fence_add_callback(sync_file->fence, &sync_file->cb,
  243. fence_check_cb_func) < 0)
  244. wake_up_all(&sync_file->wq);
  245. }
  246. return fence_is_signaled(sync_file->fence) ? POLLIN : 0;
  247. }
  248. static long sync_file_ioctl_merge(struct sync_file *sync_file,
  249. unsigned long arg)
  250. {
  251. int fd = get_unused_fd_flags(O_CLOEXEC);
  252. int err;
  253. struct sync_file *fence2, *fence3;
  254. struct sync_merge_data data;
  255. if (fd < 0)
  256. return fd;
  257. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  258. err = -EFAULT;
  259. goto err_put_fd;
  260. }
  261. if (data.flags || data.pad) {
  262. err = -EINVAL;
  263. goto err_put_fd;
  264. }
  265. fence2 = sync_file_fdget(data.fd2);
  266. if (!fence2) {
  267. err = -ENOENT;
  268. goto err_put_fd;
  269. }
  270. data.name[sizeof(data.name) - 1] = '\0';
  271. fence3 = sync_file_merge(data.name, sync_file, fence2);
  272. if (!fence3) {
  273. err = -ENOMEM;
  274. goto err_put_fence2;
  275. }
  276. data.fence = fd;
  277. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  278. err = -EFAULT;
  279. goto err_put_fence3;
  280. }
  281. fd_install(fd, fence3->file);
  282. fput(fence2->file);
  283. return 0;
  284. err_put_fence3:
  285. fput(fence3->file);
  286. err_put_fence2:
  287. fput(fence2->file);
  288. err_put_fd:
  289. put_unused_fd(fd);
  290. return err;
  291. }
  292. static void sync_fill_fence_info(struct fence *fence,
  293. struct sync_fence_info *info)
  294. {
  295. strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
  296. sizeof(info->obj_name));
  297. strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
  298. sizeof(info->driver_name));
  299. info->status = fence_get_status(fence);
  300. info->timestamp_ns = ktime_to_ns(fence->timestamp);
  301. }
  302. static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  303. unsigned long arg)
  304. {
  305. struct sync_file_info info;
  306. struct sync_fence_info *fence_info = NULL;
  307. struct fence **fences;
  308. __u32 size;
  309. int num_fences, ret, i;
  310. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  311. return -EFAULT;
  312. if (info.flags || info.pad)
  313. return -EINVAL;
  314. fences = get_fences(sync_file, &num_fences);
  315. /*
  316. * Passing num_fences = 0 means that userspace doesn't want to
  317. * retrieve any sync_fence_info. If num_fences = 0 we skip filling
  318. * sync_fence_info and return the actual number of fences on
  319. * info->num_fences.
  320. */
  321. if (!info.num_fences)
  322. goto no_fences;
  323. if (info.num_fences < num_fences)
  324. return -EINVAL;
  325. size = num_fences * sizeof(*fence_info);
  326. fence_info = kzalloc(size, GFP_KERNEL);
  327. if (!fence_info)
  328. return -ENOMEM;
  329. for (i = 0; i < num_fences; i++)
  330. sync_fill_fence_info(fences[i], &fence_info[i]);
  331. if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  332. size)) {
  333. ret = -EFAULT;
  334. goto out;
  335. }
  336. no_fences:
  337. strlcpy(info.name, sync_file->name, sizeof(info.name));
  338. info.status = fence_is_signaled(sync_file->fence);
  339. info.num_fences = num_fences;
  340. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  341. ret = -EFAULT;
  342. else
  343. ret = 0;
  344. out:
  345. kfree(fence_info);
  346. return ret;
  347. }
  348. static long sync_file_ioctl(struct file *file, unsigned int cmd,
  349. unsigned long arg)
  350. {
  351. struct sync_file *sync_file = file->private_data;
  352. switch (cmd) {
  353. case SYNC_IOC_MERGE:
  354. return sync_file_ioctl_merge(sync_file, arg);
  355. case SYNC_IOC_FILE_INFO:
  356. return sync_file_ioctl_fence_info(sync_file, arg);
  357. default:
  358. return -ENOTTY;
  359. }
  360. }
  361. static const struct file_operations sync_file_fops = {
  362. .release = sync_file_release,
  363. .poll = sync_file_poll,
  364. .unlocked_ioctl = sync_file_ioctl,
  365. .compat_ioctl = sync_file_ioctl,
  366. };