sync_file.c 11 KB

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