]>
Commit | Line | Data |
---|---|---|
d4cab38e GP |
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 | ||
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> | |
460bfc41 GP |
26 | #include <linux/sync_file.h> |
27 | #include <uapi/linux/sync_file.h> | |
d4cab38e GP |
28 | |
29 | static const struct file_operations sync_file_fops; | |
30 | ||
a02b9dc9 | 31 | static struct sync_file *sync_file_alloc(void) |
d4cab38e GP |
32 | { |
33 | struct sync_file *sync_file; | |
34 | ||
a02b9dc9 | 35 | sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL); |
d4cab38e GP |
36 | 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 | ||
a02b9dc9 GP |
48 | INIT_LIST_HEAD(&sync_file->cb.node); |
49 | ||
d4cab38e GP |
50 | return sync_file; |
51 | ||
52 | err: | |
53 | kfree(sync_file); | |
54 | return NULL; | |
55 | } | |
56 | ||
57 | static void fence_check_cb_func(struct fence *f, struct fence_cb *cb) | |
58 | { | |
d4cab38e GP |
59 | struct sync_file *sync_file; |
60 | ||
a02b9dc9 | 61 | sync_file = container_of(cb, struct sync_file, cb); |
d4cab38e | 62 | |
a02b9dc9 | 63 | wake_up_all(&sync_file->wq); |
d4cab38e GP |
64 | } |
65 | ||
66 | /** | |
c240a714 | 67 | * sync_file_create() - creates a sync file |
d4cab38e GP |
68 | * @fence: fence to add to the sync_fence |
69 | * | |
70 | * Creates a sync_file containg @fence. Once this is called, the sync_file | |
c240a714 GP |
71 | * 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. | |
d4cab38e GP |
73 | */ |
74 | struct sync_file *sync_file_create(struct fence *fence) | |
75 | { | |
76 | struct sync_file *sync_file; | |
77 | ||
a02b9dc9 | 78 | sync_file = sync_file_alloc(); |
d4cab38e GP |
79 | if (!sync_file) |
80 | return NULL; | |
81 | ||
a02b9dc9 GP |
82 | sync_file->fence = fence; |
83 | ||
041916a7 | 84 | snprintf(sync_file->name, sizeof(sync_file->name), "%s-%s%llu-%d", |
d4cab38e GP |
85 | fence->ops->get_driver_name(fence), |
86 | fence->ops->get_timeline_name(fence), fence->context, | |
87 | fence->seqno); | |
88 | ||
a02b9dc9 | 89 | fence_add_callback(fence, &sync_file->cb, fence_check_cb_func); |
d4cab38e GP |
90 | |
91 | return sync_file; | |
92 | } | |
93 | EXPORT_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 | */ | |
102 | static 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 | ||
114 | err: | |
115 | fput(file); | |
116 | return NULL; | |
117 | } | |
118 | ||
972526a4 GP |
119 | /** |
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 | */ | |
126 | struct 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 | } | |
140 | EXPORT_SYMBOL(sync_file_get_fence); | |
141 | ||
a02b9dc9 GP |
142 | static int sync_file_set_fence(struct sync_file *sync_file, |
143 | struct fence **fences, int num_fences) | |
d4cab38e | 144 | { |
a02b9dc9 GP |
145 | struct fence_array *array; |
146 | ||
147 | /* | |
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 | } | |
d4cab38e | 163 | |
a02b9dc9 GP |
164 | return 0; |
165 | } | |
166 | ||
167 | static 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 | ||
180 | static void add_fence(struct fence **fences, int *i, struct fence *fence) | |
181 | { | |
182 | fences[*i] = fence; | |
183 | ||
184 | if (!fence_is_signaled(fence)) { | |
d4cab38e GP |
185 | 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 | */ | |
200 | static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, | |
201 | struct sync_file *b) | |
202 | { | |
d4cab38e | 203 | struct sync_file *sync_file; |
a02b9dc9 GP |
204 | struct fence **fences, **nfences, **a_fences, **b_fences; |
205 | int i, i_a, i_b, num_fences, a_num_fences, b_num_fences; | |
d4cab38e | 206 | |
a02b9dc9 | 207 | sync_file = sync_file_alloc(); |
d4cab38e GP |
208 | if (!sync_file) |
209 | return NULL; | |
210 | ||
a02b9dc9 GP |
211 | 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; | |
d4cab38e GP |
221 | |
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 | */ | |
a02b9dc9 GP |
229 | 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]; | |
d4cab38e GP |
232 | |
233 | if (pt_a->context < pt_b->context) { | |
a02b9dc9 | 234 | add_fence(fences, &i, pt_a); |
d4cab38e GP |
235 | |
236 | i_a++; | |
237 | } else if (pt_a->context > pt_b->context) { | |
a02b9dc9 | 238 | add_fence(fences, &i, pt_b); |
d4cab38e GP |
239 | |
240 | i_b++; | |
241 | } else { | |
242 | if (pt_a->seqno - pt_b->seqno <= INT_MAX) | |
a02b9dc9 | 243 | add_fence(fences, &i, pt_a); |
d4cab38e | 244 | else |
a02b9dc9 | 245 | add_fence(fences, &i, pt_b); |
d4cab38e GP |
246 | |
247 | i_a++; | |
248 | i_b++; | |
249 | } | |
250 | } | |
251 | ||
a02b9dc9 GP |
252 | for (; i_a < a_num_fences; i_a++) |
253 | add_fence(fences, &i, a_fences[i_a]); | |
254 | ||
255 | for (; i_b < b_num_fences; i_b++) | |
256 | add_fence(fences, &i, b_fences[i_b]); | |
257 | ||
258 | if (i == 0) { | |
259 | add_fence(fences, &i, a_fences[0]); | |
260 | i++; | |
261 | } | |
d4cab38e | 262 | |
a02b9dc9 GP |
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 | } | |
d4cab38e | 276 | |
a02b9dc9 GP |
277 | fence_add_callback(sync_file->fence, &sync_file->cb, |
278 | fence_check_cb_func); | |
d4cab38e GP |
279 | |
280 | strlcpy(sync_file->name, name, sizeof(sync_file->name)); | |
281 | return sync_file; | |
a02b9dc9 GP |
282 | |
283 | err: | |
284 | fput(sync_file->file); | |
285 | return NULL; | |
286 | ||
d4cab38e GP |
287 | } |
288 | ||
289 | static void sync_file_free(struct kref *kref) | |
290 | { | |
291 | struct sync_file *sync_file = container_of(kref, struct sync_file, | |
292 | kref); | |
d4cab38e | 293 | |
a02b9dc9 GP |
294 | fence_remove_callback(sync_file->fence, &sync_file->cb); |
295 | fence_put(sync_file->fence); | |
d4cab38e GP |
296 | kfree(sync_file); |
297 | } | |
298 | ||
299 | static 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 | ||
307 | static 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 | ||
a02b9dc9 | 314 | status = fence_is_signaled(sync_file->fence); |
d4cab38e | 315 | |
a02b9dc9 | 316 | if (status) |
d4cab38e GP |
317 | return POLLIN; |
318 | if (status < 0) | |
319 | return POLLERR; | |
320 | return 0; | |
321 | } | |
322 | ||
323 | static long sync_file_ioctl_merge(struct sync_file *sync_file, | |
92e06213 | 324 | unsigned long arg) |
d4cab38e GP |
325 | { |
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 | ||
367 | err_put_fence3: | |
368 | fput(fence3->file); | |
369 | ||
370 | err_put_fence2: | |
371 | fput(fence2->file); | |
372 | ||
373 | err_put_fd: | |
374 | put_unused_fd(fd); | |
375 | return err; | |
376 | } | |
377 | ||
378 | static void sync_fill_fence_info(struct fence *fence, | |
92e06213 | 379 | struct sync_fence_info *info) |
d4cab38e GP |
380 | { |
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 | ||
392 | static long sync_file_ioctl_fence_info(struct sync_file *sync_file, | |
92e06213 | 393 | unsigned long arg) |
d4cab38e GP |
394 | { |
395 | struct sync_file_info info; | |
396 | struct sync_fence_info *fence_info = NULL; | |
a02b9dc9 | 397 | struct fence **fences; |
d4cab38e | 398 | __u32 size; |
a02b9dc9 | 399 | int num_fences, ret, i; |
d4cab38e GP |
400 | |
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 | ||
a02b9dc9 GP |
407 | fences = get_fences(sync_file, &num_fences); |
408 | ||
d4cab38e GP |
409 | /* |
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 | ||
a02b9dc9 | 418 | if (info.num_fences < num_fences) |
d4cab38e GP |
419 | return -EINVAL; |
420 | ||
a02b9dc9 | 421 | size = num_fences * sizeof(*fence_info); |
d4cab38e GP |
422 | fence_info = kzalloc(size, GFP_KERNEL); |
423 | if (!fence_info) | |
424 | return -ENOMEM; | |
425 | ||
a02b9dc9 GP |
426 | for (i = 0; i < num_fences; i++) |
427 | sync_fill_fence_info(fences[i], &fence_info[i]); | |
d4cab38e GP |
428 | |
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 | ||
435 | no_fences: | |
436 | strlcpy(info.name, sync_file->name, sizeof(info.name)); | |
a02b9dc9 GP |
437 | info.status = fence_is_signaled(sync_file->fence); |
438 | info.num_fences = num_fences; | |
d4cab38e GP |
439 | |
440 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) | |
441 | ret = -EFAULT; | |
442 | else | |
443 | ret = 0; | |
444 | ||
445 | out: | |
446 | kfree(fence_info); | |
447 | ||
448 | return ret; | |
449 | } | |
450 | ||
451 | static long sync_file_ioctl(struct file *file, unsigned int cmd, | |
92e06213 | 452 | unsigned long arg) |
d4cab38e GP |
453 | { |
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 | ||
468 | static 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 |