]>
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 | ||
a02b9dc9 GP |
119 | static int sync_file_set_fence(struct sync_file *sync_file, |
120 | struct fence **fences, int num_fences) | |
d4cab38e | 121 | { |
a02b9dc9 GP |
122 | struct fence_array *array; |
123 | ||
124 | /* | |
125 | * The reference for the fences in the new sync_file and held | |
126 | * in add_fence() during the merge procedure, so for num_fences == 1 | |
127 | * we already own a new reference to the fence. For num_fence > 1 | |
128 | * we own the reference of the fence_array creation. | |
129 | */ | |
130 | if (num_fences == 1) { | |
131 | sync_file->fence = fences[0]; | |
132 | } else { | |
133 | array = fence_array_create(num_fences, fences, | |
134 | fence_context_alloc(1), 1, false); | |
135 | if (!array) | |
136 | return -ENOMEM; | |
137 | ||
138 | sync_file->fence = &array->base; | |
139 | } | |
d4cab38e | 140 | |
a02b9dc9 GP |
141 | return 0; |
142 | } | |
143 | ||
144 | static struct fence **get_fences(struct sync_file *sync_file, int *num_fences) | |
145 | { | |
146 | if (fence_is_array(sync_file->fence)) { | |
147 | struct fence_array *array = to_fence_array(sync_file->fence); | |
148 | ||
149 | *num_fences = array->num_fences; | |
150 | return array->fences; | |
151 | } | |
152 | ||
153 | *num_fences = 1; | |
154 | return &sync_file->fence; | |
155 | } | |
156 | ||
157 | static void add_fence(struct fence **fences, int *i, struct fence *fence) | |
158 | { | |
159 | fences[*i] = fence; | |
160 | ||
161 | if (!fence_is_signaled(fence)) { | |
d4cab38e GP |
162 | fence_get(fence); |
163 | (*i)++; | |
164 | } | |
165 | } | |
166 | ||
167 | /** | |
168 | * sync_file_merge() - merge two sync_files | |
169 | * @name: name of new fence | |
170 | * @a: sync_file a | |
171 | * @b: sync_file b | |
172 | * | |
173 | * Creates a new sync_file which contains copies of all the fences in both | |
174 | * @a and @b. @a and @b remain valid, independent sync_file. Returns the | |
175 | * new merged sync_file or NULL in case of error. | |
176 | */ | |
177 | static struct sync_file *sync_file_merge(const char *name, struct sync_file *a, | |
178 | struct sync_file *b) | |
179 | { | |
d4cab38e | 180 | struct sync_file *sync_file; |
a02b9dc9 GP |
181 | struct fence **fences, **nfences, **a_fences, **b_fences; |
182 | int i, i_a, i_b, num_fences, a_num_fences, b_num_fences; | |
d4cab38e | 183 | |
a02b9dc9 | 184 | sync_file = sync_file_alloc(); |
d4cab38e GP |
185 | if (!sync_file) |
186 | return NULL; | |
187 | ||
a02b9dc9 GP |
188 | a_fences = get_fences(a, &a_num_fences); |
189 | b_fences = get_fences(b, &b_num_fences); | |
190 | if (a_num_fences > INT_MAX - b_num_fences) | |
191 | return NULL; | |
192 | ||
193 | num_fences = a_num_fences + b_num_fences; | |
194 | ||
195 | fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL); | |
196 | if (!fences) | |
197 | goto err; | |
d4cab38e GP |
198 | |
199 | /* | |
200 | * Assume sync_file a and b are both ordered and have no | |
201 | * duplicates with the same context. | |
202 | * | |
203 | * If a sync_file can only be created with sync_file_merge | |
204 | * and sync_file_create, this is a reasonable assumption. | |
205 | */ | |
a02b9dc9 GP |
206 | for (i = i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) { |
207 | struct fence *pt_a = a_fences[i_a]; | |
208 | struct fence *pt_b = b_fences[i_b]; | |
d4cab38e GP |
209 | |
210 | if (pt_a->context < pt_b->context) { | |
a02b9dc9 | 211 | add_fence(fences, &i, pt_a); |
d4cab38e GP |
212 | |
213 | i_a++; | |
214 | } else if (pt_a->context > pt_b->context) { | |
a02b9dc9 | 215 | add_fence(fences, &i, pt_b); |
d4cab38e GP |
216 | |
217 | i_b++; | |
218 | } else { | |
219 | if (pt_a->seqno - pt_b->seqno <= INT_MAX) | |
a02b9dc9 | 220 | add_fence(fences, &i, pt_a); |
d4cab38e | 221 | else |
a02b9dc9 | 222 | add_fence(fences, &i, pt_b); |
d4cab38e GP |
223 | |
224 | i_a++; | |
225 | i_b++; | |
226 | } | |
227 | } | |
228 | ||
a02b9dc9 GP |
229 | for (; i_a < a_num_fences; i_a++) |
230 | add_fence(fences, &i, a_fences[i_a]); | |
231 | ||
232 | for (; i_b < b_num_fences; i_b++) | |
233 | add_fence(fences, &i, b_fences[i_b]); | |
234 | ||
235 | if (i == 0) { | |
236 | add_fence(fences, &i, a_fences[0]); | |
237 | i++; | |
238 | } | |
d4cab38e | 239 | |
a02b9dc9 GP |
240 | if (num_fences > i) { |
241 | nfences = krealloc(fences, i * sizeof(*fences), | |
242 | GFP_KERNEL); | |
243 | if (!nfences) | |
244 | goto err; | |
245 | ||
246 | fences = nfences; | |
247 | } | |
248 | ||
249 | if (sync_file_set_fence(sync_file, fences, i) < 0) { | |
250 | kfree(fences); | |
251 | goto err; | |
252 | } | |
d4cab38e | 253 | |
a02b9dc9 GP |
254 | fence_add_callback(sync_file->fence, &sync_file->cb, |
255 | fence_check_cb_func); | |
d4cab38e GP |
256 | |
257 | strlcpy(sync_file->name, name, sizeof(sync_file->name)); | |
258 | return sync_file; | |
a02b9dc9 GP |
259 | |
260 | err: | |
261 | fput(sync_file->file); | |
262 | return NULL; | |
263 | ||
d4cab38e GP |
264 | } |
265 | ||
266 | static void sync_file_free(struct kref *kref) | |
267 | { | |
268 | struct sync_file *sync_file = container_of(kref, struct sync_file, | |
269 | kref); | |
d4cab38e | 270 | |
a02b9dc9 GP |
271 | fence_remove_callback(sync_file->fence, &sync_file->cb); |
272 | fence_put(sync_file->fence); | |
d4cab38e GP |
273 | kfree(sync_file); |
274 | } | |
275 | ||
276 | static int sync_file_release(struct inode *inode, struct file *file) | |
277 | { | |
278 | struct sync_file *sync_file = file->private_data; | |
279 | ||
280 | kref_put(&sync_file->kref, sync_file_free); | |
281 | return 0; | |
282 | } | |
283 | ||
284 | static unsigned int sync_file_poll(struct file *file, poll_table *wait) | |
285 | { | |
286 | struct sync_file *sync_file = file->private_data; | |
287 | int status; | |
288 | ||
289 | poll_wait(file, &sync_file->wq, wait); | |
290 | ||
a02b9dc9 | 291 | status = fence_is_signaled(sync_file->fence); |
d4cab38e | 292 | |
a02b9dc9 | 293 | if (status) |
d4cab38e GP |
294 | return POLLIN; |
295 | if (status < 0) | |
296 | return POLLERR; | |
297 | return 0; | |
298 | } | |
299 | ||
300 | static long sync_file_ioctl_merge(struct sync_file *sync_file, | |
92e06213 | 301 | unsigned long arg) |
d4cab38e GP |
302 | { |
303 | int fd = get_unused_fd_flags(O_CLOEXEC); | |
304 | int err; | |
305 | struct sync_file *fence2, *fence3; | |
306 | struct sync_merge_data data; | |
307 | ||
308 | if (fd < 0) | |
309 | return fd; | |
310 | ||
311 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) { | |
312 | err = -EFAULT; | |
313 | goto err_put_fd; | |
314 | } | |
315 | ||
316 | if (data.flags || data.pad) { | |
317 | err = -EINVAL; | |
318 | goto err_put_fd; | |
319 | } | |
320 | ||
321 | fence2 = sync_file_fdget(data.fd2); | |
322 | if (!fence2) { | |
323 | err = -ENOENT; | |
324 | goto err_put_fd; | |
325 | } | |
326 | ||
327 | data.name[sizeof(data.name) - 1] = '\0'; | |
328 | fence3 = sync_file_merge(data.name, sync_file, fence2); | |
329 | if (!fence3) { | |
330 | err = -ENOMEM; | |
331 | goto err_put_fence2; | |
332 | } | |
333 | ||
334 | data.fence = fd; | |
335 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { | |
336 | err = -EFAULT; | |
337 | goto err_put_fence3; | |
338 | } | |
339 | ||
340 | fd_install(fd, fence3->file); | |
341 | fput(fence2->file); | |
342 | return 0; | |
343 | ||
344 | err_put_fence3: | |
345 | fput(fence3->file); | |
346 | ||
347 | err_put_fence2: | |
348 | fput(fence2->file); | |
349 | ||
350 | err_put_fd: | |
351 | put_unused_fd(fd); | |
352 | return err; | |
353 | } | |
354 | ||
355 | static void sync_fill_fence_info(struct fence *fence, | |
92e06213 | 356 | struct sync_fence_info *info) |
d4cab38e GP |
357 | { |
358 | strlcpy(info->obj_name, fence->ops->get_timeline_name(fence), | |
359 | sizeof(info->obj_name)); | |
360 | strlcpy(info->driver_name, fence->ops->get_driver_name(fence), | |
361 | sizeof(info->driver_name)); | |
362 | if (fence_is_signaled(fence)) | |
363 | info->status = fence->status >= 0 ? 1 : fence->status; | |
364 | else | |
365 | info->status = 0; | |
366 | info->timestamp_ns = ktime_to_ns(fence->timestamp); | |
367 | } | |
368 | ||
369 | static long sync_file_ioctl_fence_info(struct sync_file *sync_file, | |
92e06213 | 370 | unsigned long arg) |
d4cab38e GP |
371 | { |
372 | struct sync_file_info info; | |
373 | struct sync_fence_info *fence_info = NULL; | |
a02b9dc9 | 374 | struct fence **fences; |
d4cab38e | 375 | __u32 size; |
a02b9dc9 | 376 | int num_fences, ret, i; |
d4cab38e GP |
377 | |
378 | if (copy_from_user(&info, (void __user *)arg, sizeof(info))) | |
379 | return -EFAULT; | |
380 | ||
381 | if (info.flags || info.pad) | |
382 | return -EINVAL; | |
383 | ||
a02b9dc9 GP |
384 | fences = get_fences(sync_file, &num_fences); |
385 | ||
d4cab38e GP |
386 | /* |
387 | * Passing num_fences = 0 means that userspace doesn't want to | |
388 | * retrieve any sync_fence_info. If num_fences = 0 we skip filling | |
389 | * sync_fence_info and return the actual number of fences on | |
390 | * info->num_fences. | |
391 | */ | |
392 | if (!info.num_fences) | |
393 | goto no_fences; | |
394 | ||
a02b9dc9 | 395 | if (info.num_fences < num_fences) |
d4cab38e GP |
396 | return -EINVAL; |
397 | ||
a02b9dc9 | 398 | size = num_fences * sizeof(*fence_info); |
d4cab38e GP |
399 | fence_info = kzalloc(size, GFP_KERNEL); |
400 | if (!fence_info) | |
401 | return -ENOMEM; | |
402 | ||
a02b9dc9 GP |
403 | for (i = 0; i < num_fences; i++) |
404 | sync_fill_fence_info(fences[i], &fence_info[i]); | |
d4cab38e GP |
405 | |
406 | if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info, | |
407 | size)) { | |
408 | ret = -EFAULT; | |
409 | goto out; | |
410 | } | |
411 | ||
412 | no_fences: | |
413 | strlcpy(info.name, sync_file->name, sizeof(info.name)); | |
a02b9dc9 GP |
414 | info.status = fence_is_signaled(sync_file->fence); |
415 | info.num_fences = num_fences; | |
d4cab38e GP |
416 | |
417 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) | |
418 | ret = -EFAULT; | |
419 | else | |
420 | ret = 0; | |
421 | ||
422 | out: | |
423 | kfree(fence_info); | |
424 | ||
425 | return ret; | |
426 | } | |
427 | ||
428 | static long sync_file_ioctl(struct file *file, unsigned int cmd, | |
92e06213 | 429 | unsigned long arg) |
d4cab38e GP |
430 | { |
431 | struct sync_file *sync_file = file->private_data; | |
432 | ||
433 | switch (cmd) { | |
434 | case SYNC_IOC_MERGE: | |
435 | return sync_file_ioctl_merge(sync_file, arg); | |
436 | ||
437 | case SYNC_IOC_FILE_INFO: | |
438 | return sync_file_ioctl_fence_info(sync_file, arg); | |
439 | ||
440 | default: | |
441 | return -ENOTTY; | |
442 | } | |
443 | } | |
444 | ||
445 | static const struct file_operations sync_file_fops = { | |
446 | .release = sync_file_release, | |
447 | .poll = sync_file_poll, | |
448 | .unlocked_ioctl = sync_file_ioctl, | |
449 | .compat_ioctl = sync_file_ioctl, | |
450 | }; | |
451 |