sync_file.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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(int size)
  29. {
  30. struct sync_file *sync_file;
  31. sync_file = kzalloc(size, 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. return sync_file;
  41. err:
  42. kfree(sync_file);
  43. return NULL;
  44. }
  45. static void fence_check_cb_func(struct fence *f, struct fence_cb *cb)
  46. {
  47. struct sync_file_cb *check;
  48. struct sync_file *sync_file;
  49. check = container_of(cb, struct sync_file_cb, cb);
  50. sync_file = check->sync_file;
  51. if (atomic_dec_and_test(&sync_file->status))
  52. wake_up_all(&sync_file->wq);
  53. }
  54. /**
  55. * sync_file_create() - creates a sync file
  56. * @fence: fence to add to the sync_fence
  57. *
  58. * Creates a sync_file containg @fence. Once this is called, the sync_file
  59. * takes ownership of @fence. The sync_file can be released with
  60. * fput(sync_file->file). Returns the sync_file or NULL in case of error.
  61. */
  62. struct sync_file *sync_file_create(struct fence *fence)
  63. {
  64. struct sync_file *sync_file;
  65. sync_file = sync_file_alloc(offsetof(struct sync_file, cbs[1]));
  66. if (!sync_file)
  67. return NULL;
  68. sync_file->num_fences = 1;
  69. atomic_set(&sync_file->status, 1);
  70. snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d",
  71. fence->ops->get_driver_name(fence),
  72. fence->ops->get_timeline_name(fence), fence->context,
  73. fence->seqno);
  74. sync_file->cbs[0].fence = fence;
  75. sync_file->cbs[0].sync_file = sync_file;
  76. if (fence_add_callback(fence, &sync_file->cbs[0].cb,
  77. fence_check_cb_func))
  78. atomic_dec(&sync_file->status);
  79. return sync_file;
  80. }
  81. EXPORT_SYMBOL(sync_file_create);
  82. /**
  83. * sync_file_fdget() - get a sync_file from an fd
  84. * @fd: fd referencing a fence
  85. *
  86. * Ensures @fd references a valid sync_file, increments the refcount of the
  87. * backing file. Returns the sync_file or NULL in case of error.
  88. */
  89. static struct sync_file *sync_file_fdget(int fd)
  90. {
  91. struct file *file = fget(fd);
  92. if (!file)
  93. return NULL;
  94. if (file->f_op != &sync_file_fops)
  95. goto err;
  96. return file->private_data;
  97. err:
  98. fput(file);
  99. return NULL;
  100. }
  101. static void sync_file_add_pt(struct sync_file *sync_file, int *i,
  102. struct fence *fence)
  103. {
  104. sync_file->cbs[*i].fence = fence;
  105. sync_file->cbs[*i].sync_file = sync_file;
  106. if (!fence_add_callback(fence, &sync_file->cbs[*i].cb,
  107. fence_check_cb_func)) {
  108. fence_get(fence);
  109. (*i)++;
  110. }
  111. }
  112. /**
  113. * sync_file_merge() - merge two sync_files
  114. * @name: name of new fence
  115. * @a: sync_file a
  116. * @b: sync_file b
  117. *
  118. * Creates a new sync_file which contains copies of all the fences in both
  119. * @a and @b. @a and @b remain valid, independent sync_file. Returns the
  120. * new merged sync_file or NULL in case of error.
  121. */
  122. static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
  123. struct sync_file *b)
  124. {
  125. int num_fences = a->num_fences + b->num_fences;
  126. struct sync_file *sync_file;
  127. int i, i_a, i_b;
  128. unsigned long size = offsetof(struct sync_file, cbs[num_fences]);
  129. sync_file = sync_file_alloc(size);
  130. if (!sync_file)
  131. return NULL;
  132. atomic_set(&sync_file->status, num_fences);
  133. /*
  134. * Assume sync_file a and b are both ordered and have no
  135. * duplicates with the same context.
  136. *
  137. * If a sync_file can only be created with sync_file_merge
  138. * and sync_file_create, this is a reasonable assumption.
  139. */
  140. for (i = i_a = i_b = 0; i_a < a->num_fences && i_b < b->num_fences; ) {
  141. struct fence *pt_a = a->cbs[i_a].fence;
  142. struct fence *pt_b = b->cbs[i_b].fence;
  143. if (pt_a->context < pt_b->context) {
  144. sync_file_add_pt(sync_file, &i, pt_a);
  145. i_a++;
  146. } else if (pt_a->context > pt_b->context) {
  147. sync_file_add_pt(sync_file, &i, pt_b);
  148. i_b++;
  149. } else {
  150. if (pt_a->seqno - pt_b->seqno <= INT_MAX)
  151. sync_file_add_pt(sync_file, &i, pt_a);
  152. else
  153. sync_file_add_pt(sync_file, &i, pt_b);
  154. i_a++;
  155. i_b++;
  156. }
  157. }
  158. for (; i_a < a->num_fences; i_a++)
  159. sync_file_add_pt(sync_file, &i, a->cbs[i_a].fence);
  160. for (; i_b < b->num_fences; i_b++)
  161. sync_file_add_pt(sync_file, &i, b->cbs[i_b].fence);
  162. if (num_fences > i)
  163. atomic_sub(num_fences - i, &sync_file->status);
  164. sync_file->num_fences = i;
  165. strlcpy(sync_file->name, name, sizeof(sync_file->name));
  166. return sync_file;
  167. }
  168. static void sync_file_free(struct kref *kref)
  169. {
  170. struct sync_file *sync_file = container_of(kref, struct sync_file,
  171. kref);
  172. int i;
  173. for (i = 0; i < sync_file->num_fences; ++i) {
  174. fence_remove_callback(sync_file->cbs[i].fence,
  175. &sync_file->cbs[i].cb);
  176. fence_put(sync_file->cbs[i].fence);
  177. }
  178. kfree(sync_file);
  179. }
  180. static int sync_file_release(struct inode *inode, struct file *file)
  181. {
  182. struct sync_file *sync_file = file->private_data;
  183. kref_put(&sync_file->kref, sync_file_free);
  184. return 0;
  185. }
  186. static unsigned int sync_file_poll(struct file *file, poll_table *wait)
  187. {
  188. struct sync_file *sync_file = file->private_data;
  189. int status;
  190. poll_wait(file, &sync_file->wq, wait);
  191. status = atomic_read(&sync_file->status);
  192. if (!status)
  193. return POLLIN;
  194. if (status < 0)
  195. return POLLERR;
  196. return 0;
  197. }
  198. static long sync_file_ioctl_merge(struct sync_file *sync_file,
  199. unsigned long arg)
  200. {
  201. int fd = get_unused_fd_flags(O_CLOEXEC);
  202. int err;
  203. struct sync_file *fence2, *fence3;
  204. struct sync_merge_data data;
  205. if (fd < 0)
  206. return fd;
  207. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  208. err = -EFAULT;
  209. goto err_put_fd;
  210. }
  211. if (data.flags || data.pad) {
  212. err = -EINVAL;
  213. goto err_put_fd;
  214. }
  215. fence2 = sync_file_fdget(data.fd2);
  216. if (!fence2) {
  217. err = -ENOENT;
  218. goto err_put_fd;
  219. }
  220. data.name[sizeof(data.name) - 1] = '\0';
  221. fence3 = sync_file_merge(data.name, sync_file, fence2);
  222. if (!fence3) {
  223. err = -ENOMEM;
  224. goto err_put_fence2;
  225. }
  226. data.fence = fd;
  227. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  228. err = -EFAULT;
  229. goto err_put_fence3;
  230. }
  231. fd_install(fd, fence3->file);
  232. fput(fence2->file);
  233. return 0;
  234. err_put_fence3:
  235. fput(fence3->file);
  236. err_put_fence2:
  237. fput(fence2->file);
  238. err_put_fd:
  239. put_unused_fd(fd);
  240. return err;
  241. }
  242. static void sync_fill_fence_info(struct fence *fence,
  243. struct sync_fence_info *info)
  244. {
  245. strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
  246. sizeof(info->obj_name));
  247. strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
  248. sizeof(info->driver_name));
  249. if (fence_is_signaled(fence))
  250. info->status = fence->status >= 0 ? 1 : fence->status;
  251. else
  252. info->status = 0;
  253. info->timestamp_ns = ktime_to_ns(fence->timestamp);
  254. }
  255. static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  256. unsigned long arg)
  257. {
  258. struct sync_file_info info;
  259. struct sync_fence_info *fence_info = NULL;
  260. __u32 size;
  261. int ret, i;
  262. if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
  263. return -EFAULT;
  264. if (info.flags || info.pad)
  265. return -EINVAL;
  266. /*
  267. * Passing num_fences = 0 means that userspace doesn't want to
  268. * retrieve any sync_fence_info. If num_fences = 0 we skip filling
  269. * sync_fence_info and return the actual number of fences on
  270. * info->num_fences.
  271. */
  272. if (!info.num_fences)
  273. goto no_fences;
  274. if (info.num_fences < sync_file->num_fences)
  275. return -EINVAL;
  276. size = sync_file->num_fences * sizeof(*fence_info);
  277. fence_info = kzalloc(size, GFP_KERNEL);
  278. if (!fence_info)
  279. return -ENOMEM;
  280. for (i = 0; i < sync_file->num_fences; ++i)
  281. sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]);
  282. if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  283. size)) {
  284. ret = -EFAULT;
  285. goto out;
  286. }
  287. no_fences:
  288. strlcpy(info.name, sync_file->name, sizeof(info.name));
  289. info.status = atomic_read(&sync_file->status);
  290. if (info.status >= 0)
  291. info.status = !info.status;
  292. info.num_fences = sync_file->num_fences;
  293. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  294. ret = -EFAULT;
  295. else
  296. ret = 0;
  297. out:
  298. kfree(fence_info);
  299. return ret;
  300. }
  301. static long sync_file_ioctl(struct file *file, unsigned int cmd,
  302. unsigned long arg)
  303. {
  304. struct sync_file *sync_file = file->private_data;
  305. switch (cmd) {
  306. case SYNC_IOC_MERGE:
  307. return sync_file_ioctl_merge(sync_file, arg);
  308. case SYNC_IOC_FILE_INFO:
  309. return sync_file_ioctl_fence_info(sync_file, arg);
  310. default:
  311. return -ENOTTY;
  312. }
  313. }
  314. static const struct file_operations sync_file_fops = {
  315. .release = sync_file_release,
  316. .poll = sync_file_poll,
  317. .unlocked_ioctl = sync_file_ioctl,
  318. .compat_ioctl = sync_file_ioctl,
  319. };