blob: 2873760e02a9f4c71d0fea1b21a1746f2d5047a9 [file] [log] [blame]
Gustavo Padovand4cab382016-04-28 10:46:54 -03001/*
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
17#include <linux/export.h>
18#include <linux/file.h>
19#include <linux/fs.h>
20#include <linux/kernel.h>
21#include <linux/poll.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/uaccess.h>
25#include <linux/anon_inodes.h>
Gustavo Padovan460bfc42016-04-28 10:46:57 -030026#include <linux/sync_file.h>
27#include <uapi/linux/sync_file.h>
Gustavo Padovand4cab382016-04-28 10:46:54 -030028
29static const struct file_operations sync_file_fops;
30
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030031static struct sync_file *sync_file_alloc(void)
Gustavo Padovand4cab382016-04-28 10:46:54 -030032{
33 struct sync_file *sync_file;
34
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030035 sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL);
Gustavo Padovand4cab382016-04-28 10:46:54 -030036 if (!sync_file)
37 return NULL;
38
39 sync_file->file = anon_inode_getfile("sync_file", &sync_file_fops,
40 sync_file, 0);
41 if (IS_ERR(sync_file->file))
42 goto err;
43
44 kref_init(&sync_file->kref);
45
46 init_waitqueue_head(&sync_file->wq);
47
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030048 INIT_LIST_HEAD(&sync_file->cb.node);
49
Gustavo Padovand4cab382016-04-28 10:46:54 -030050 return sync_file;
51
52err:
53 kfree(sync_file);
54 return NULL;
55}
56
57static void fence_check_cb_func(struct fence *f, struct fence_cb *cb)
58{
Gustavo Padovand4cab382016-04-28 10:46:54 -030059 struct sync_file *sync_file;
60
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030061 sync_file = container_of(cb, struct sync_file, cb);
Gustavo Padovand4cab382016-04-28 10:46:54 -030062
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030063 wake_up_all(&sync_file->wq);
Gustavo Padovand4cab382016-04-28 10:46:54 -030064}
65
66/**
Gustavo Padovanc240a712016-04-28 10:46:55 -030067 * sync_file_create() - creates a sync file
Gustavo Padovand4cab382016-04-28 10:46:54 -030068 * @fence: fence to add to the sync_fence
69 *
70 * Creates a sync_file containg @fence. Once this is called, the sync_file
Gustavo Padovanc240a712016-04-28 10:46:55 -030071 * takes ownership of @fence. The sync_file can be released with
72 * fput(sync_file->file). Returns the sync_file or NULL in case of error.
Gustavo Padovand4cab382016-04-28 10:46:54 -030073 */
74struct sync_file *sync_file_create(struct fence *fence)
75{
76 struct sync_file *sync_file;
77
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030078 sync_file = sync_file_alloc();
Gustavo Padovand4cab382016-04-28 10:46:54 -030079 if (!sync_file)
80 return NULL;
81
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030082 sync_file->fence = fence;
83
Gustavo Padovan041916a2016-06-03 12:46:31 -030084 snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d",
Gustavo Padovand4cab382016-04-28 10:46:54 -030085 fence->ops->get_driver_name(fence),
86 fence->ops->get_timeline_name(fence), fence->context,
87 fence->seqno);
88
Gustavo Padovana02b9dc2016-08-05 10:39:35 -030089 fence_add_callback(fence, &sync_file->cb, fence_check_cb_func);
Gustavo Padovand4cab382016-04-28 10:46:54 -030090
91 return sync_file;
92}
93EXPORT_SYMBOL(sync_file_create);
94
95/**
96 * sync_file_fdget() - get a sync_file from an fd
97 * @fd: fd referencing a fence
98 *
99 * Ensures @fd references a valid sync_file, increments the refcount of the
100 * backing file. Returns the sync_file or NULL in case of error.
101 */
102static struct sync_file *sync_file_fdget(int fd)
103{
104 struct file *file = fget(fd);
105
106 if (!file)
107 return NULL;
108
109 if (file->f_op != &sync_file_fops)
110 goto err;
111
112 return file->private_data;
113
114err:
115 fput(file);
116 return NULL;
117}
118
Gustavo Padovan972526a2016-08-05 10:39:36 -0300119/**
120 * sync_file_get_fence - get the fence related to the sync_file fd
121 * @fd: sync_file fd to get the fence from
122 *
123 * Ensures @fd references a valid sync_file and returns a fence that
124 * represents all fence in the sync_file. On error NULL is returned.
125 */
126struct fence *sync_file_get_fence(int fd)
127{
128 struct sync_file *sync_file;
129 struct fence *fence;
130
131 sync_file = sync_file_fdget(fd);
132 if (!sync_file)
133 return NULL;
134
135 fence = fence_get(sync_file->fence);
136 fput(sync_file->file);
137
138 return fence;
139}
140EXPORT_SYMBOL(sync_file_get_fence);
141
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300142static int sync_file_set_fence(struct sync_file *sync_file,
143 struct fence **fences, int num_fences)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300144{
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300145 struct fence_array *array;
Gustavo Padovand4cab382016-04-28 10:46:54 -0300146
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300147 /*
148 * The reference for the fences in the new sync_file and held
149 * in add_fence() during the merge procedure, so for num_fences == 1
150 * we already own a new reference to the fence. For num_fence > 1
151 * we own the reference of the fence_array creation.
152 */
153 if (num_fences == 1) {
154 sync_file->fence = fences[0];
155 } else {
156 array = fence_array_create(num_fences, fences,
157 fence_context_alloc(1), 1, false);
158 if (!array)
159 return -ENOMEM;
160
161 sync_file->fence = &array->base;
162 }
163
164 return 0;
165}
166
167static struct fence **get_fences(struct sync_file *sync_file, int *num_fences)
168{
169 if (fence_is_array(sync_file->fence)) {
170 struct fence_array *array = to_fence_array(sync_file->fence);
171
172 *num_fences = array->num_fences;
173 return array->fences;
174 }
175
176 *num_fences = 1;
177 return &sync_file->fence;
178}
179
180static void add_fence(struct fence **fences, int *i, struct fence *fence)
181{
182 fences[*i] = fence;
183
184 if (!fence_is_signaled(fence)) {
Gustavo Padovand4cab382016-04-28 10:46:54 -0300185 fence_get(fence);
186 (*i)++;
187 }
188}
189
190/**
191 * sync_file_merge() - merge two sync_files
192 * @name: name of new fence
193 * @a: sync_file a
194 * @b: sync_file b
195 *
196 * Creates a new sync_file which contains copies of all the fences in both
197 * @a and @b. @a and @b remain valid, independent sync_file. Returns the
198 * new merged sync_file or NULL in case of error.
199 */
200static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
201 struct sync_file *b)
202{
Gustavo Padovand4cab382016-04-28 10:46:54 -0300203 struct sync_file *sync_file;
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300204 struct fence **fences, **nfences, **a_fences, **b_fences;
205 int i, i_a, i_b, num_fences, a_num_fences, b_num_fences;
Gustavo Padovand4cab382016-04-28 10:46:54 -0300206
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300207 sync_file = sync_file_alloc();
Gustavo Padovand4cab382016-04-28 10:46:54 -0300208 if (!sync_file)
209 return NULL;
210
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300211 a_fences = get_fences(a, &a_num_fences);
212 b_fences = get_fences(b, &b_num_fences);
213 if (a_num_fences > INT_MAX - b_num_fences)
214 return NULL;
215
216 num_fences = a_num_fences + b_num_fences;
217
218 fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
219 if (!fences)
220 goto err;
Gustavo Padovand4cab382016-04-28 10:46:54 -0300221
222 /*
223 * Assume sync_file a and b are both ordered and have no
224 * duplicates with the same context.
225 *
226 * If a sync_file can only be created with sync_file_merge
227 * and sync_file_create, this is a reasonable assumption.
228 */
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300229 for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) {
230 struct fence *pt_a = a_fences[i_a];
231 struct fence *pt_b = b_fences[i_b];
Gustavo Padovand4cab382016-04-28 10:46:54 -0300232
233 if (pt_a->context < pt_b->context) {
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300234 add_fence(fences, &i, pt_a);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300235
236 i_a++;
237 } else if (pt_a->context > pt_b->context) {
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300238 add_fence(fences, &i, pt_b);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300239
240 i_b++;
241 } else {
242 if (pt_a->seqno - pt_b->seqno <= INT_MAX)
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300243 add_fence(fences, &i, pt_a);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300244 else
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300245 add_fence(fences, &i, pt_b);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300246
247 i_a++;
248 i_b++;
249 }
250 }
251
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300252 for (; i_a < a_num_fences; i_a++)
253 add_fence(fences, &i, a_fences[i_a]);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300254
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300255 for (; i_b < b_num_fences; i_b++)
256 add_fence(fences, &i, b_fences[i_b]);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300257
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300258 if (i == 0) {
259 add_fence(fences, &i, a_fences[0]);
260 i++;
261 }
262
263 if (num_fences > i) {
264 nfences = krealloc(fences, i * sizeof(*fences),
265 GFP_KERNEL);
266 if (!nfences)
267 goto err;
268
269 fences = nfences;
270 }
271
272 if (sync_file_set_fence(sync_file, fences, i) < 0) {
273 kfree(fences);
274 goto err;
275 }
276
277 fence_add_callback(sync_file->fence, &sync_file->cb,
278 fence_check_cb_func);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300279
280 strlcpy(sync_file->name, name, sizeof(sync_file->name));
281 return sync_file;
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300282
283err:
284 fput(sync_file->file);
285 return NULL;
286
Gustavo Padovand4cab382016-04-28 10:46:54 -0300287}
288
289static void sync_file_free(struct kref *kref)
290{
291 struct sync_file *sync_file = container_of(kref, struct sync_file,
292 kref);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300293
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300294 fence_remove_callback(sync_file->fence, &sync_file->cb);
295 fence_put(sync_file->fence);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300296 kfree(sync_file);
297}
298
299static int sync_file_release(struct inode *inode, struct file *file)
300{
301 struct sync_file *sync_file = file->private_data;
302
303 kref_put(&sync_file->kref, sync_file_free);
304 return 0;
305}
306
307static unsigned int sync_file_poll(struct file *file, poll_table *wait)
308{
309 struct sync_file *sync_file = file->private_data;
310 int status;
311
312 poll_wait(file, &sync_file->wq, wait);
313
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300314 status = fence_is_signaled(sync_file->fence);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300315
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300316 if (status)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300317 return POLLIN;
318 if (status < 0)
319 return POLLERR;
320 return 0;
321}
322
323static long sync_file_ioctl_merge(struct sync_file *sync_file,
Gustavo Padovan92e06212016-04-28 10:46:56 -0300324 unsigned long arg)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300325{
326 int fd = get_unused_fd_flags(O_CLOEXEC);
327 int err;
328 struct sync_file *fence2, *fence3;
329 struct sync_merge_data data;
330
331 if (fd < 0)
332 return fd;
333
334 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
335 err = -EFAULT;
336 goto err_put_fd;
337 }
338
339 if (data.flags || data.pad) {
340 err = -EINVAL;
341 goto err_put_fd;
342 }
343
344 fence2 = sync_file_fdget(data.fd2);
345 if (!fence2) {
346 err = -ENOENT;
347 goto err_put_fd;
348 }
349
350 data.name[sizeof(data.name) - 1] = '\0';
351 fence3 = sync_file_merge(data.name, sync_file, fence2);
352 if (!fence3) {
353 err = -ENOMEM;
354 goto err_put_fence2;
355 }
356
357 data.fence = fd;
358 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
359 err = -EFAULT;
360 goto err_put_fence3;
361 }
362
363 fd_install(fd, fence3->file);
364 fput(fence2->file);
365 return 0;
366
367err_put_fence3:
368 fput(fence3->file);
369
370err_put_fence2:
371 fput(fence2->file);
372
373err_put_fd:
374 put_unused_fd(fd);
375 return err;
376}
377
378static void sync_fill_fence_info(struct fence *fence,
Gustavo Padovan92e06212016-04-28 10:46:56 -0300379 struct sync_fence_info *info)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300380{
381 strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
382 sizeof(info->obj_name));
383 strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
384 sizeof(info->driver_name));
385 if (fence_is_signaled(fence))
386 info->status = fence->status >= 0 ? 1 : fence->status;
387 else
388 info->status = 0;
389 info->timestamp_ns = ktime_to_ns(fence->timestamp);
390}
391
392static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
Gustavo Padovan92e06212016-04-28 10:46:56 -0300393 unsigned long arg)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300394{
395 struct sync_file_info info;
396 struct sync_fence_info *fence_info = NULL;
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300397 struct fence **fences;
Gustavo Padovand4cab382016-04-28 10:46:54 -0300398 __u32 size;
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300399 int num_fences, ret, i;
Gustavo Padovand4cab382016-04-28 10:46:54 -0300400
401 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
402 return -EFAULT;
403
404 if (info.flags || info.pad)
405 return -EINVAL;
406
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300407 fences = get_fences(sync_file, &num_fences);
408
Gustavo Padovand4cab382016-04-28 10:46:54 -0300409 /*
410 * Passing num_fences = 0 means that userspace doesn't want to
411 * retrieve any sync_fence_info. If num_fences = 0 we skip filling
412 * sync_fence_info and return the actual number of fences on
413 * info->num_fences.
414 */
415 if (!info.num_fences)
416 goto no_fences;
417
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300418 if (info.num_fences < num_fences)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300419 return -EINVAL;
420
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300421 size = num_fences * sizeof(*fence_info);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300422 fence_info = kzalloc(size, GFP_KERNEL);
423 if (!fence_info)
424 return -ENOMEM;
425
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300426 for (i = 0; i < num_fences; i++)
427 sync_fill_fence_info(fences[i], &fence_info[i]);
Gustavo Padovand4cab382016-04-28 10:46:54 -0300428
429 if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
430 size)) {
431 ret = -EFAULT;
432 goto out;
433 }
434
435no_fences:
436 strlcpy(info.name, sync_file->name, sizeof(info.name));
Gustavo Padovana02b9dc2016-08-05 10:39:35 -0300437 info.status = fence_is_signaled(sync_file->fence);
438 info.num_fences = num_fences;
Gustavo Padovand4cab382016-04-28 10:46:54 -0300439
440 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
441 ret = -EFAULT;
442 else
443 ret = 0;
444
445out:
446 kfree(fence_info);
447
448 return ret;
449}
450
451static long sync_file_ioctl(struct file *file, unsigned int cmd,
Gustavo Padovan92e06212016-04-28 10:46:56 -0300452 unsigned long arg)
Gustavo Padovand4cab382016-04-28 10:46:54 -0300453{
454 struct sync_file *sync_file = file->private_data;
455
456 switch (cmd) {
457 case SYNC_IOC_MERGE:
458 return sync_file_ioctl_merge(sync_file, arg);
459
460 case SYNC_IOC_FILE_INFO:
461 return sync_file_ioctl_fence_info(sync_file, arg);
462
463 default:
464 return -ENOTTY;
465 }
466}
467
468static const struct file_operations sync_file_fops = {
469 .release = sync_file_release,
470 .poll = sync_file_poll,
471 .unlocked_ioctl = sync_file_ioctl,
472 .compat_ioctl = sync_file_ioctl,
473};
474