Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / dma-buf / sync_file.c
blob9aaa608dfe0111515e01b59227a7ca87d72ab61b
1 /*
2 * drivers/dma-buf/sync_file.c
4 * Copyright (C) 2012 Google, Inc.
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.
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.
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>
26 #include <linux/sync_file.h>
27 #include <uapi/linux/sync_file.h>
29 static const struct file_operations sync_file_fops;
31 static struct sync_file *sync_file_alloc(int size)
33 struct sync_file *sync_file;
35 sync_file = kzalloc(size, GFP_KERNEL);
36 if (!sync_file)
37 return NULL;
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;
44 kref_init(&sync_file->kref);
46 init_waitqueue_head(&sync_file->wq);
48 return sync_file;
50 err:
51 kfree(sync_file);
52 return NULL;
55 static void fence_check_cb_func(struct fence *f, struct fence_cb *cb)
57 struct sync_file_cb *check;
58 struct sync_file *sync_file;
60 check = container_of(cb, struct sync_file_cb, cb);
61 sync_file = check->sync_file;
63 if (atomic_dec_and_test(&sync_file->status))
64 wake_up_all(&sync_file->wq);
67 /**
68 * sync_file_create() - creates a sync file
69 * @fence: fence to add to the sync_fence
71 * Creates a sync_file containg @fence. Once this is called, the sync_file
72 * takes ownership of @fence. The sync_file can be released with
73 * fput(sync_file->file). Returns the sync_file or NULL in case of error.
75 struct sync_file *sync_file_create(struct fence *fence)
77 struct sync_file *sync_file;
79 sync_file = sync_file_alloc(offsetof(struct sync_file, cbs[1]));
80 if (!sync_file)
81 return NULL;
83 sync_file->num_fences = 1;
84 atomic_set(&sync_file->status, 1);
85 snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d",
86 fence->ops->get_driver_name(fence),
87 fence->ops->get_timeline_name(fence), fence->context,
88 fence->seqno);
90 sync_file->cbs[0].fence = fence;
91 sync_file->cbs[0].sync_file = sync_file;
92 if (fence_add_callback(fence, &sync_file->cbs[0].cb,
93 fence_check_cb_func))
94 atomic_dec(&sync_file->status);
96 return sync_file;
98 EXPORT_SYMBOL(sync_file_create);
101 * sync_file_fdget() - get a sync_file from an fd
102 * @fd: fd referencing a fence
104 * Ensures @fd references a valid sync_file, increments the refcount of the
105 * backing file. Returns the sync_file or NULL in case of error.
107 static struct sync_file *sync_file_fdget(int fd)
109 struct file *file = fget(fd);
111 if (!file)
112 return NULL;
114 if (file->f_op != &sync_file_fops)
115 goto err;
117 return file->private_data;
119 err:
120 fput(file);
121 return NULL;
124 static void sync_file_add_pt(struct sync_file *sync_file, int *i,
125 struct fence *fence)
127 sync_file->cbs[*i].fence = fence;
128 sync_file->cbs[*i].sync_file = sync_file;
130 if (!fence_add_callback(fence, &sync_file->cbs[*i].cb,
131 fence_check_cb_func)) {
132 fence_get(fence);
133 (*i)++;
138 * sync_file_merge() - merge two sync_files
139 * @name: name of new fence
140 * @a: sync_file a
141 * @b: sync_file b
143 * Creates a new sync_file which contains copies of all the fences in both
144 * @a and @b. @a and @b remain valid, independent sync_file. Returns the
145 * new merged sync_file or NULL in case of error.
147 static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
148 struct sync_file *b)
150 int num_fences = a->num_fences + b->num_fences;
151 struct sync_file *sync_file;
152 int i, i_a, i_b;
153 unsigned long size = offsetof(struct sync_file, cbs[num_fences]);
155 sync_file = sync_file_alloc(size);
156 if (!sync_file)
157 return NULL;
159 atomic_set(&sync_file->status, num_fences);
162 * Assume sync_file a and b are both ordered and have no
163 * duplicates with the same context.
165 * If a sync_file can only be created with sync_file_merge
166 * and sync_file_create, this is a reasonable assumption.
168 for (i = i_a = i_b = 0; i_a < a->num_fences && i_b < b->num_fences; ) {
169 struct fence *pt_a = a->cbs[i_a].fence;
170 struct fence *pt_b = b->cbs[i_b].fence;
172 if (pt_a->context < pt_b->context) {
173 sync_file_add_pt(sync_file, &i, pt_a);
175 i_a++;
176 } else if (pt_a->context > pt_b->context) {
177 sync_file_add_pt(sync_file, &i, pt_b);
179 i_b++;
180 } else {
181 if (pt_a->seqno - pt_b->seqno <= INT_MAX)
182 sync_file_add_pt(sync_file, &i, pt_a);
183 else
184 sync_file_add_pt(sync_file, &i, pt_b);
186 i_a++;
187 i_b++;
191 for (; i_a < a->num_fences; i_a++)
192 sync_file_add_pt(sync_file, &i, a->cbs[i_a].fence);
194 for (; i_b < b->num_fences; i_b++)
195 sync_file_add_pt(sync_file, &i, b->cbs[i_b].fence);
197 if (num_fences > i)
198 atomic_sub(num_fences - i, &sync_file->status);
199 sync_file->num_fences = i;
201 strlcpy(sync_file->name, name, sizeof(sync_file->name));
202 return sync_file;
205 static void sync_file_free(struct kref *kref)
207 struct sync_file *sync_file = container_of(kref, struct sync_file,
208 kref);
209 int i;
211 for (i = 0; i < sync_file->num_fences; ++i) {
212 fence_remove_callback(sync_file->cbs[i].fence,
213 &sync_file->cbs[i].cb);
214 fence_put(sync_file->cbs[i].fence);
217 kfree(sync_file);
220 static int sync_file_release(struct inode *inode, struct file *file)
222 struct sync_file *sync_file = file->private_data;
224 kref_put(&sync_file->kref, sync_file_free);
225 return 0;
228 static unsigned int sync_file_poll(struct file *file, poll_table *wait)
230 struct sync_file *sync_file = file->private_data;
231 int status;
233 poll_wait(file, &sync_file->wq, wait);
235 status = atomic_read(&sync_file->status);
237 if (!status)
238 return POLLIN;
239 if (status < 0)
240 return POLLERR;
241 return 0;
244 static long sync_file_ioctl_merge(struct sync_file *sync_file,
245 unsigned long arg)
247 int fd = get_unused_fd_flags(O_CLOEXEC);
248 int err;
249 struct sync_file *fence2, *fence3;
250 struct sync_merge_data data;
252 if (fd < 0)
253 return fd;
255 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
256 err = -EFAULT;
257 goto err_put_fd;
260 if (data.flags || data.pad) {
261 err = -EINVAL;
262 goto err_put_fd;
265 fence2 = sync_file_fdget(data.fd2);
266 if (!fence2) {
267 err = -ENOENT;
268 goto err_put_fd;
271 data.name[sizeof(data.name) - 1] = '\0';
272 fence3 = sync_file_merge(data.name, sync_file, fence2);
273 if (!fence3) {
274 err = -ENOMEM;
275 goto err_put_fence2;
278 data.fence = fd;
279 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
280 err = -EFAULT;
281 goto err_put_fence3;
284 fd_install(fd, fence3->file);
285 fput(fence2->file);
286 return 0;
288 err_put_fence3:
289 fput(fence3->file);
291 err_put_fence2:
292 fput(fence2->file);
294 err_put_fd:
295 put_unused_fd(fd);
296 return err;
299 static void sync_fill_fence_info(struct fence *fence,
300 struct sync_fence_info *info)
302 strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
303 sizeof(info->obj_name));
304 strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
305 sizeof(info->driver_name));
306 if (fence_is_signaled(fence))
307 info->status = fence->status >= 0 ? 1 : fence->status;
308 else
309 info->status = 0;
310 info->timestamp_ns = ktime_to_ns(fence->timestamp);
313 static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
314 unsigned long arg)
316 struct sync_file_info info;
317 struct sync_fence_info *fence_info = NULL;
318 __u32 size;
319 int ret, i;
321 if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
322 return -EFAULT;
324 if (info.flags || info.pad)
325 return -EINVAL;
328 * Passing num_fences = 0 means that userspace doesn't want to
329 * retrieve any sync_fence_info. If num_fences = 0 we skip filling
330 * sync_fence_info and return the actual number of fences on
331 * info->num_fences.
333 if (!info.num_fences)
334 goto no_fences;
336 if (info.num_fences < sync_file->num_fences)
337 return -EINVAL;
339 size = sync_file->num_fences * sizeof(*fence_info);
340 fence_info = kzalloc(size, GFP_KERNEL);
341 if (!fence_info)
342 return -ENOMEM;
344 for (i = 0; i < sync_file->num_fences; ++i)
345 sync_fill_fence_info(sync_file->cbs[i].fence, &fence_info[i]);
347 if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
348 size)) {
349 ret = -EFAULT;
350 goto out;
353 no_fences:
354 strlcpy(info.name, sync_file->name, sizeof(info.name));
355 info.status = atomic_read(&sync_file->status);
356 if (info.status >= 0)
357 info.status = !info.status;
359 info.num_fences = sync_file->num_fences;
361 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
362 ret = -EFAULT;
363 else
364 ret = 0;
366 out:
367 kfree(fence_info);
369 return ret;
372 static long sync_file_ioctl(struct file *file, unsigned int cmd,
373 unsigned long arg)
375 struct sync_file *sync_file = file->private_data;
377 switch (cmd) {
378 case SYNC_IOC_MERGE:
379 return sync_file_ioctl_merge(sync_file, arg);
381 case SYNC_IOC_FILE_INFO:
382 return sync_file_ioctl_fence_info(sync_file, arg);
384 default:
385 return -ENOTTY;
389 static const struct file_operations sync_file_fops = {
390 .release = sync_file_release,
391 .poll = sync_file_poll,
392 .unlocked_ioctl = sync_file_ioctl,
393 .compat_ioctl = sync_file_ioctl,