1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * drivers/dma-buf/sync_file.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 Google, Inc.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/export.h>
9*4882a593Smuzhiyun #include <linux/file.h>
10*4882a593Smuzhiyun #include <linux/fs.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/poll.h>
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/uaccess.h>
16*4882a593Smuzhiyun #include <linux/anon_inodes.h>
17*4882a593Smuzhiyun #include <linux/sync_file.h>
18*4882a593Smuzhiyun #include <uapi/linux/sync_file.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static const struct file_operations sync_file_fops;
21*4882a593Smuzhiyun
sync_file_alloc(void)22*4882a593Smuzhiyun static struct sync_file *sync_file_alloc(void)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun struct sync_file *sync_file;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun sync_file = kzalloc(sizeof(*sync_file), GFP_KERNEL);
27*4882a593Smuzhiyun if (!sync_file)
28*4882a593Smuzhiyun return NULL;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun sync_file->file = anon_inode_getfile("sync_file", &sync_file_fops,
31*4882a593Smuzhiyun sync_file, 0);
32*4882a593Smuzhiyun if (IS_ERR(sync_file->file))
33*4882a593Smuzhiyun goto err;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun init_waitqueue_head(&sync_file->wq);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun INIT_LIST_HEAD(&sync_file->cb.node);
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun return sync_file;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun err:
42*4882a593Smuzhiyun kfree(sync_file);
43*4882a593Smuzhiyun return NULL;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
fence_check_cb_func(struct dma_fence * f,struct dma_fence_cb * cb)46*4882a593Smuzhiyun static void fence_check_cb_func(struct dma_fence *f, struct dma_fence_cb *cb)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct sync_file *sync_file;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun sync_file = container_of(cb, struct sync_file, cb);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun wake_up_all(&sync_file->wq);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun * sync_file_create() - creates a sync file
57*4882a593Smuzhiyun * @fence: fence to add to the sync_fence
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * Creates a sync_file containg @fence. This function acquires and additional
60*4882a593Smuzhiyun * reference of @fence for the newly-created &sync_file, if it succeeds. The
61*4882a593Smuzhiyun * sync_file can be released with fput(sync_file->file). Returns the
62*4882a593Smuzhiyun * sync_file or NULL in case of error.
63*4882a593Smuzhiyun */
sync_file_create(struct dma_fence * fence)64*4882a593Smuzhiyun struct sync_file *sync_file_create(struct dma_fence *fence)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct sync_file *sync_file;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun sync_file = sync_file_alloc();
69*4882a593Smuzhiyun if (!sync_file)
70*4882a593Smuzhiyun return NULL;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun sync_file->fence = dma_fence_get(fence);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun return sync_file;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun EXPORT_SYMBOL(sync_file_create);
77*4882a593Smuzhiyun
sync_file_fdget(int fd)78*4882a593Smuzhiyun static struct sync_file *sync_file_fdget(int fd)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun struct file *file = fget(fd);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (!file)
83*4882a593Smuzhiyun return NULL;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (file->f_op != &sync_file_fops)
86*4882a593Smuzhiyun goto err;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return file->private_data;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun err:
91*4882a593Smuzhiyun fput(file);
92*4882a593Smuzhiyun return NULL;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun * sync_file_get_fence - get the fence related to the sync_file fd
97*4882a593Smuzhiyun * @fd: sync_file fd to get the fence from
98*4882a593Smuzhiyun *
99*4882a593Smuzhiyun * Ensures @fd references a valid sync_file and returns a fence that
100*4882a593Smuzhiyun * represents all fence in the sync_file. On error NULL is returned.
101*4882a593Smuzhiyun */
sync_file_get_fence(int fd)102*4882a593Smuzhiyun struct dma_fence *sync_file_get_fence(int fd)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct sync_file *sync_file;
105*4882a593Smuzhiyun struct dma_fence *fence;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun sync_file = sync_file_fdget(fd);
108*4882a593Smuzhiyun if (!sync_file)
109*4882a593Smuzhiyun return NULL;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun fence = dma_fence_get(sync_file->fence);
112*4882a593Smuzhiyun fput(sync_file->file);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return fence;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun EXPORT_SYMBOL(sync_file_get_fence);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun * sync_file_get_name - get the name of the sync_file
120*4882a593Smuzhiyun * @sync_file: sync_file to get the fence from
121*4882a593Smuzhiyun * @buf: destination buffer to copy sync_file name into
122*4882a593Smuzhiyun * @len: available size of destination buffer.
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * Each sync_file may have a name assigned either by the user (when merging
125*4882a593Smuzhiyun * sync_files together) or created from the fence it contains. In the latter
126*4882a593Smuzhiyun * case construction of the name is deferred until use, and so requires
127*4882a593Smuzhiyun * sync_file_get_name().
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * Returns: a string representing the name.
130*4882a593Smuzhiyun */
sync_file_get_name(struct sync_file * sync_file,char * buf,int len)131*4882a593Smuzhiyun char *sync_file_get_name(struct sync_file *sync_file, char *buf, int len)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun if (sync_file->user_name[0]) {
134*4882a593Smuzhiyun strlcpy(buf, sync_file->user_name, len);
135*4882a593Smuzhiyun } else {
136*4882a593Smuzhiyun struct dma_fence *fence = sync_file->fence;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun snprintf(buf, len, "%s-%s%llu-%lld",
139*4882a593Smuzhiyun fence->ops->get_driver_name(fence),
140*4882a593Smuzhiyun fence->ops->get_timeline_name(fence),
141*4882a593Smuzhiyun fence->context,
142*4882a593Smuzhiyun fence->seqno);
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun return buf;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
sync_file_set_fence(struct sync_file * sync_file,struct dma_fence ** fences,int num_fences)148*4882a593Smuzhiyun static int sync_file_set_fence(struct sync_file *sync_file,
149*4882a593Smuzhiyun struct dma_fence **fences, int num_fences)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun struct dma_fence_array *array;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * The reference for the fences in the new sync_file and held
155*4882a593Smuzhiyun * in add_fence() during the merge procedure, so for num_fences == 1
156*4882a593Smuzhiyun * we already own a new reference to the fence. For num_fence > 1
157*4882a593Smuzhiyun * we own the reference of the dma_fence_array creation.
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun if (num_fences == 1) {
160*4882a593Smuzhiyun sync_file->fence = fences[0];
161*4882a593Smuzhiyun kfree(fences);
162*4882a593Smuzhiyun } else {
163*4882a593Smuzhiyun array = dma_fence_array_create(num_fences, fences,
164*4882a593Smuzhiyun dma_fence_context_alloc(1),
165*4882a593Smuzhiyun 1, false);
166*4882a593Smuzhiyun if (!array)
167*4882a593Smuzhiyun return -ENOMEM;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun sync_file->fence = &array->base;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun return 0;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
get_fences(struct sync_file * sync_file,int * num_fences)175*4882a593Smuzhiyun static struct dma_fence **get_fences(struct sync_file *sync_file,
176*4882a593Smuzhiyun int *num_fences)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun if (dma_fence_is_array(sync_file->fence)) {
179*4882a593Smuzhiyun struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun *num_fences = array->num_fences;
182*4882a593Smuzhiyun return array->fences;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun *num_fences = 1;
186*4882a593Smuzhiyun return &sync_file->fence;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
add_fence(struct dma_fence ** fences,int * i,struct dma_fence * fence)189*4882a593Smuzhiyun static void add_fence(struct dma_fence **fences,
190*4882a593Smuzhiyun int *i, struct dma_fence *fence)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun fences[*i] = fence;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (!dma_fence_is_signaled(fence)) {
195*4882a593Smuzhiyun dma_fence_get(fence);
196*4882a593Smuzhiyun (*i)++;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun * sync_file_merge() - merge two sync_files
202*4882a593Smuzhiyun * @name: name of new fence
203*4882a593Smuzhiyun * @a: sync_file a
204*4882a593Smuzhiyun * @b: sync_file b
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * Creates a new sync_file which contains copies of all the fences in both
207*4882a593Smuzhiyun * @a and @b. @a and @b remain valid, independent sync_file. Returns the
208*4882a593Smuzhiyun * new merged sync_file or NULL in case of error.
209*4882a593Smuzhiyun */
sync_file_merge(const char * name,struct sync_file * a,struct sync_file * b)210*4882a593Smuzhiyun static struct sync_file *sync_file_merge(const char *name, struct sync_file *a,
211*4882a593Smuzhiyun struct sync_file *b)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun struct sync_file *sync_file;
214*4882a593Smuzhiyun struct dma_fence **fences = NULL, **nfences, **a_fences, **b_fences;
215*4882a593Smuzhiyun int i = 0, i_a, i_b, num_fences, a_num_fences, b_num_fences;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun sync_file = sync_file_alloc();
218*4882a593Smuzhiyun if (!sync_file)
219*4882a593Smuzhiyun return NULL;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun a_fences = get_fences(a, &a_num_fences);
222*4882a593Smuzhiyun b_fences = get_fences(b, &b_num_fences);
223*4882a593Smuzhiyun if (a_num_fences > INT_MAX - b_num_fences)
224*4882a593Smuzhiyun goto err;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun num_fences = a_num_fences + b_num_fences;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun fences = kcalloc(num_fences, sizeof(*fences), GFP_KERNEL);
229*4882a593Smuzhiyun if (!fences)
230*4882a593Smuzhiyun goto err;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /*
233*4882a593Smuzhiyun * Assume sync_file a and b are both ordered and have no
234*4882a593Smuzhiyun * duplicates with the same context.
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * If a sync_file can only be created with sync_file_merge
237*4882a593Smuzhiyun * and sync_file_create, this is a reasonable assumption.
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun for (i_a = i_b = 0; i_a < a_num_fences && i_b < b_num_fences; ) {
240*4882a593Smuzhiyun struct dma_fence *pt_a = a_fences[i_a];
241*4882a593Smuzhiyun struct dma_fence *pt_b = b_fences[i_b];
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (pt_a->context < pt_b->context) {
244*4882a593Smuzhiyun add_fence(fences, &i, pt_a);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun i_a++;
247*4882a593Smuzhiyun } else if (pt_a->context > pt_b->context) {
248*4882a593Smuzhiyun add_fence(fences, &i, pt_b);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun i_b++;
251*4882a593Smuzhiyun } else {
252*4882a593Smuzhiyun if (__dma_fence_is_later(pt_a->seqno, pt_b->seqno,
253*4882a593Smuzhiyun pt_a->ops))
254*4882a593Smuzhiyun add_fence(fences, &i, pt_a);
255*4882a593Smuzhiyun else
256*4882a593Smuzhiyun add_fence(fences, &i, pt_b);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun i_a++;
259*4882a593Smuzhiyun i_b++;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun for (; i_a < a_num_fences; i_a++)
264*4882a593Smuzhiyun add_fence(fences, &i, a_fences[i_a]);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun for (; i_b < b_num_fences; i_b++)
267*4882a593Smuzhiyun add_fence(fences, &i, b_fences[i_b]);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (i == 0)
270*4882a593Smuzhiyun fences[i++] = dma_fence_get(a_fences[0]);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun if (num_fences > i) {
273*4882a593Smuzhiyun nfences = krealloc(fences, i * sizeof(*fences),
274*4882a593Smuzhiyun GFP_KERNEL);
275*4882a593Smuzhiyun if (!nfences)
276*4882a593Smuzhiyun goto err;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun fences = nfences;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun if (sync_file_set_fence(sync_file, fences, i) < 0)
282*4882a593Smuzhiyun goto err;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun strlcpy(sync_file->user_name, name, sizeof(sync_file->user_name));
285*4882a593Smuzhiyun return sync_file;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun err:
288*4882a593Smuzhiyun while (i)
289*4882a593Smuzhiyun dma_fence_put(fences[--i]);
290*4882a593Smuzhiyun kfree(fences);
291*4882a593Smuzhiyun fput(sync_file->file);
292*4882a593Smuzhiyun return NULL;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
sync_file_release(struct inode * inode,struct file * file)296*4882a593Smuzhiyun static int sync_file_release(struct inode *inode, struct file *file)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun struct sync_file *sync_file = file->private_data;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun if (test_bit(POLL_ENABLED, &sync_file->flags))
301*4882a593Smuzhiyun dma_fence_remove_callback(sync_file->fence, &sync_file->cb);
302*4882a593Smuzhiyun dma_fence_put(sync_file->fence);
303*4882a593Smuzhiyun kfree(sync_file);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun return 0;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
sync_file_poll(struct file * file,poll_table * wait)308*4882a593Smuzhiyun static __poll_t sync_file_poll(struct file *file, poll_table *wait)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun struct sync_file *sync_file = file->private_data;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun poll_wait(file, &sync_file->wq, wait);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun if (list_empty(&sync_file->cb.node) &&
315*4882a593Smuzhiyun !test_and_set_bit(POLL_ENABLED, &sync_file->flags)) {
316*4882a593Smuzhiyun if (dma_fence_add_callback(sync_file->fence, &sync_file->cb,
317*4882a593Smuzhiyun fence_check_cb_func) < 0)
318*4882a593Smuzhiyun wake_up_all(&sync_file->wq);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun return dma_fence_is_signaled(sync_file->fence) ? EPOLLIN : 0;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
sync_file_ioctl_merge(struct sync_file * sync_file,unsigned long arg)324*4882a593Smuzhiyun static long sync_file_ioctl_merge(struct sync_file *sync_file,
325*4882a593Smuzhiyun unsigned long arg)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun int fd = get_unused_fd_flags(O_CLOEXEC);
328*4882a593Smuzhiyun int err;
329*4882a593Smuzhiyun struct sync_file *fence2, *fence3;
330*4882a593Smuzhiyun struct sync_merge_data data;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (fd < 0)
333*4882a593Smuzhiyun return fd;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
336*4882a593Smuzhiyun err = -EFAULT;
337*4882a593Smuzhiyun goto err_put_fd;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun if (data.flags || data.pad) {
341*4882a593Smuzhiyun err = -EINVAL;
342*4882a593Smuzhiyun goto err_put_fd;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun fence2 = sync_file_fdget(data.fd2);
346*4882a593Smuzhiyun if (!fence2) {
347*4882a593Smuzhiyun err = -ENOENT;
348*4882a593Smuzhiyun goto err_put_fd;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun data.name[sizeof(data.name) - 1] = '\0';
352*4882a593Smuzhiyun fence3 = sync_file_merge(data.name, sync_file, fence2);
353*4882a593Smuzhiyun if (!fence3) {
354*4882a593Smuzhiyun err = -ENOMEM;
355*4882a593Smuzhiyun goto err_put_fence2;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun data.fence = fd;
359*4882a593Smuzhiyun if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
360*4882a593Smuzhiyun err = -EFAULT;
361*4882a593Smuzhiyun goto err_put_fence3;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun fd_install(fd, fence3->file);
365*4882a593Smuzhiyun fput(fence2->file);
366*4882a593Smuzhiyun return 0;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun err_put_fence3:
369*4882a593Smuzhiyun fput(fence3->file);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun err_put_fence2:
372*4882a593Smuzhiyun fput(fence2->file);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun err_put_fd:
375*4882a593Smuzhiyun put_unused_fd(fd);
376*4882a593Smuzhiyun return err;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
sync_fill_fence_info(struct dma_fence * fence,struct sync_fence_info * info)379*4882a593Smuzhiyun static int sync_fill_fence_info(struct dma_fence *fence,
380*4882a593Smuzhiyun struct sync_fence_info *info)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
383*4882a593Smuzhiyun sizeof(info->obj_name));
384*4882a593Smuzhiyun strlcpy(info->driver_name, fence->ops->get_driver_name(fence),
385*4882a593Smuzhiyun sizeof(info->driver_name));
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun info->status = dma_fence_get_status(fence);
388*4882a593Smuzhiyun while (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) &&
389*4882a593Smuzhiyun !test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
390*4882a593Smuzhiyun cpu_relax();
391*4882a593Smuzhiyun info->timestamp_ns =
392*4882a593Smuzhiyun test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags) ?
393*4882a593Smuzhiyun ktime_to_ns(fence->timestamp) :
394*4882a593Smuzhiyun ktime_set(0, 0);
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun return info->status;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
sync_file_ioctl_fence_info(struct sync_file * sync_file,unsigned long arg)399*4882a593Smuzhiyun static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
400*4882a593Smuzhiyun unsigned long arg)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun struct sync_file_info info;
403*4882a593Smuzhiyun struct sync_fence_info *fence_info = NULL;
404*4882a593Smuzhiyun struct dma_fence **fences;
405*4882a593Smuzhiyun __u32 size;
406*4882a593Smuzhiyun int num_fences, ret, i;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
409*4882a593Smuzhiyun return -EFAULT;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun if (info.flags || info.pad)
412*4882a593Smuzhiyun return -EINVAL;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun fences = get_fences(sync_file, &num_fences);
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun /*
417*4882a593Smuzhiyun * Passing num_fences = 0 means that userspace doesn't want to
418*4882a593Smuzhiyun * retrieve any sync_fence_info. If num_fences = 0 we skip filling
419*4882a593Smuzhiyun * sync_fence_info and return the actual number of fences on
420*4882a593Smuzhiyun * info->num_fences.
421*4882a593Smuzhiyun */
422*4882a593Smuzhiyun if (!info.num_fences) {
423*4882a593Smuzhiyun info.status = dma_fence_get_status(sync_file->fence);
424*4882a593Smuzhiyun goto no_fences;
425*4882a593Smuzhiyun } else {
426*4882a593Smuzhiyun info.status = 1;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun if (info.num_fences < num_fences)
430*4882a593Smuzhiyun return -EINVAL;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun size = num_fences * sizeof(*fence_info);
433*4882a593Smuzhiyun fence_info = kzalloc(size, GFP_KERNEL);
434*4882a593Smuzhiyun if (!fence_info)
435*4882a593Smuzhiyun return -ENOMEM;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun for (i = 0; i < num_fences; i++) {
438*4882a593Smuzhiyun int status = sync_fill_fence_info(fences[i], &fence_info[i]);
439*4882a593Smuzhiyun info.status = info.status <= 0 ? info.status : status;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
443*4882a593Smuzhiyun size)) {
444*4882a593Smuzhiyun ret = -EFAULT;
445*4882a593Smuzhiyun goto out;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun no_fences:
449*4882a593Smuzhiyun sync_file_get_name(sync_file, info.name, sizeof(info.name));
450*4882a593Smuzhiyun info.num_fences = num_fences;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun if (copy_to_user((void __user *)arg, &info, sizeof(info)))
453*4882a593Smuzhiyun ret = -EFAULT;
454*4882a593Smuzhiyun else
455*4882a593Smuzhiyun ret = 0;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun out:
458*4882a593Smuzhiyun kfree(fence_info);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun return ret;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
sync_file_ioctl(struct file * file,unsigned int cmd,unsigned long arg)463*4882a593Smuzhiyun static long sync_file_ioctl(struct file *file, unsigned int cmd,
464*4882a593Smuzhiyun unsigned long arg)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun struct sync_file *sync_file = file->private_data;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun switch (cmd) {
469*4882a593Smuzhiyun case SYNC_IOC_MERGE:
470*4882a593Smuzhiyun return sync_file_ioctl_merge(sync_file, arg);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun case SYNC_IOC_FILE_INFO:
473*4882a593Smuzhiyun return sync_file_ioctl_fence_info(sync_file, arg);
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun default:
476*4882a593Smuzhiyun return -ENOTTY;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun static const struct file_operations sync_file_fops = {
481*4882a593Smuzhiyun .release = sync_file_release,
482*4882a593Smuzhiyun .poll = sync_file_poll,
483*4882a593Smuzhiyun .unlocked_ioctl = sync_file_ioctl,
484*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
485*4882a593Smuzhiyun };
486