2 * Sync File validation framework
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/file.h>
19 #include <linux/uaccess.h>
20 #include <linux/slab.h>
21 #include <linux/sync_file.h>
23 #include "sync_debug.h"
25 #define CREATE_TRACE_POINTS
26 #include "sync_trace.h"
29 * SW SYNC validation framework
31 * A sync object driver that uses a 32bit counter to coordinate
32 * synchronization. Useful when there is no hardware primitive backing
33 * the synchronization.
35 * To start the framework just open:
37 * <debugfs>/sync/sw_sync
39 * That will create a sync timeline, all fences created under this timeline
40 * file descriptor will belong to the this timeline.
42 * The 'sw_sync' file can be opened many times as to create different
45 * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
46 * sw_sync_create_fence_data as parameter.
48 * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
49 * with the increment as u32. This will update the last signaled value
50 * from the timeline and signal any fence that has a seqno smaller or equal
53 * struct sw_sync_create_fence_data
54 * @value: the seqno to initialise the fence with
55 * @name: the name of the new sync point
56 * @fence: return the fd of the new sync_file with the created fence
58 struct sw_sync_create_fence_data
{
61 __s32 fence
; /* fd of new fence */
64 #define SW_SYNC_IOC_MAGIC 'W'
66 #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
67 struct sw_sync_create_fence_data)
69 #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
71 static const struct dma_fence_ops timeline_fence_ops
;
73 static inline struct sync_pt
*dma_fence_to_sync_pt(struct dma_fence
*fence
)
75 if (fence
->ops
!= &timeline_fence_ops
)
77 return container_of(fence
, struct sync_pt
, base
);
81 * sync_timeline_create() - creates a sync object
82 * @name: sync_timeline name
84 * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
87 static struct sync_timeline
*sync_timeline_create(const char *name
)
89 struct sync_timeline
*obj
;
91 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
95 kref_init(&obj
->kref
);
96 obj
->context
= dma_fence_context_alloc(1);
97 strlcpy(obj
->name
, name
, sizeof(obj
->name
));
99 obj
->pt_tree
= RB_ROOT
;
100 INIT_LIST_HEAD(&obj
->pt_list
);
101 spin_lock_init(&obj
->lock
);
103 sync_timeline_debug_add(obj
);
108 static void sync_timeline_free(struct kref
*kref
)
110 struct sync_timeline
*obj
=
111 container_of(kref
, struct sync_timeline
, kref
);
113 sync_timeline_debug_remove(obj
);
118 static void sync_timeline_get(struct sync_timeline
*obj
)
120 kref_get(&obj
->kref
);
123 static void sync_timeline_put(struct sync_timeline
*obj
)
125 kref_put(&obj
->kref
, sync_timeline_free
);
128 static const char *timeline_fence_get_driver_name(struct dma_fence
*fence
)
133 static const char *timeline_fence_get_timeline_name(struct dma_fence
*fence
)
135 struct sync_timeline
*parent
= dma_fence_parent(fence
);
140 static void timeline_fence_release(struct dma_fence
*fence
)
142 struct sync_pt
*pt
= dma_fence_to_sync_pt(fence
);
143 struct sync_timeline
*parent
= dma_fence_parent(fence
);
146 spin_lock_irqsave(fence
->lock
, flags
);
147 if (!list_empty(&pt
->link
)) {
149 rb_erase(&pt
->node
, &parent
->pt_tree
);
151 spin_unlock_irqrestore(fence
->lock
, flags
);
153 sync_timeline_put(parent
);
154 dma_fence_free(fence
);
157 static bool timeline_fence_signaled(struct dma_fence
*fence
)
159 struct sync_timeline
*parent
= dma_fence_parent(fence
);
161 return !__dma_fence_is_later(fence
->seqno
, parent
->value
);
164 static bool timeline_fence_enable_signaling(struct dma_fence
*fence
)
169 static void timeline_fence_value_str(struct dma_fence
*fence
,
172 snprintf(str
, size
, "%d", fence
->seqno
);
175 static void timeline_fence_timeline_value_str(struct dma_fence
*fence
,
178 struct sync_timeline
*parent
= dma_fence_parent(fence
);
180 snprintf(str
, size
, "%d", parent
->value
);
183 static const struct dma_fence_ops timeline_fence_ops
= {
184 .get_driver_name
= timeline_fence_get_driver_name
,
185 .get_timeline_name
= timeline_fence_get_timeline_name
,
186 .enable_signaling
= timeline_fence_enable_signaling
,
187 .signaled
= timeline_fence_signaled
,
188 .release
= timeline_fence_release
,
189 .fence_value_str
= timeline_fence_value_str
,
190 .timeline_value_str
= timeline_fence_timeline_value_str
,
194 * sync_timeline_signal() - signal a status change on a sync_timeline
195 * @obj: sync_timeline to signal
196 * @inc: num to increment on timeline->value
198 * A sync implementation should call this any time one of it's fences
199 * has signaled or has an error condition.
201 static void sync_timeline_signal(struct sync_timeline
*obj
, unsigned int inc
)
203 struct sync_pt
*pt
, *next
;
205 trace_sync_timeline(obj
);
207 spin_lock_irq(&obj
->lock
);
211 list_for_each_entry_safe(pt
, next
, &obj
->pt_list
, link
) {
212 if (!timeline_fence_signaled(&pt
->base
))
215 list_del_init(&pt
->link
);
216 rb_erase(&pt
->node
, &obj
->pt_tree
);
219 * A signal callback may release the last reference to this
220 * fence, causing it to be freed. That operation has to be
221 * last to avoid a use after free inside this loop, and must
222 * be after we remove the fence from the timeline in order to
223 * prevent deadlocking on timeline->lock inside
224 * timeline_fence_release().
226 dma_fence_signal_locked(&pt
->base
);
229 spin_unlock_irq(&obj
->lock
);
233 * sync_pt_create() - creates a sync pt
234 * @obj: parent sync_timeline
235 * @value: value of the fence
237 * Creates a new sync_pt (fence) as a child of @parent. @size bytes will be
238 * allocated allowing for implementation specific data to be kept after
239 * the generic sync_timeline struct. Returns the sync_pt object or
240 * NULL in case of error.
242 static struct sync_pt
*sync_pt_create(struct sync_timeline
*obj
,
247 pt
= kzalloc(sizeof(*pt
), GFP_KERNEL
);
251 sync_timeline_get(obj
);
252 dma_fence_init(&pt
->base
, &timeline_fence_ops
, &obj
->lock
,
253 obj
->context
, value
);
254 INIT_LIST_HEAD(&pt
->link
);
256 spin_lock_irq(&obj
->lock
);
257 if (!dma_fence_is_signaled_locked(&pt
->base
)) {
258 struct rb_node
**p
= &obj
->pt_tree
.rb_node
;
259 struct rb_node
*parent
= NULL
;
262 struct sync_pt
*other
;
266 other
= rb_entry(parent
, typeof(*pt
), node
);
267 cmp
= value
- other
->base
.seqno
;
269 p
= &parent
->rb_right
;
270 } else if (cmp
< 0) {
271 p
= &parent
->rb_left
;
273 if (dma_fence_get_rcu(&other
->base
)) {
274 sync_timeline_put(obj
);
279 p
= &parent
->rb_left
;
282 rb_link_node(&pt
->node
, parent
, p
);
283 rb_insert_color(&pt
->node
, &obj
->pt_tree
);
285 parent
= rb_next(&pt
->node
);
286 list_add_tail(&pt
->link
,
287 parent
? &rb_entry(parent
, typeof(*pt
), node
)->link
: &obj
->pt_list
);
290 spin_unlock_irq(&obj
->lock
);
298 * improper use of this can result in deadlocking kernel drivers from userspace.
301 /* opening sw_sync create a new sync obj */
302 static int sw_sync_debugfs_open(struct inode
*inode
, struct file
*file
)
304 struct sync_timeline
*obj
;
305 char task_comm
[TASK_COMM_LEN
];
307 get_task_comm(task_comm
, current
);
309 obj
= sync_timeline_create(task_comm
);
313 file
->private_data
= obj
;
318 static int sw_sync_debugfs_release(struct inode
*inode
, struct file
*file
)
320 struct sync_timeline
*obj
= file
->private_data
;
321 struct sync_pt
*pt
, *next
;
323 spin_lock_irq(&obj
->lock
);
325 list_for_each_entry_safe(pt
, next
, &obj
->pt_list
, link
) {
326 dma_fence_set_error(&pt
->base
, -ENOENT
);
327 dma_fence_signal_locked(&pt
->base
);
330 spin_unlock_irq(&obj
->lock
);
332 sync_timeline_put(obj
);
336 static long sw_sync_ioctl_create_fence(struct sync_timeline
*obj
,
339 int fd
= get_unused_fd_flags(O_CLOEXEC
);
342 struct sync_file
*sync_file
;
343 struct sw_sync_create_fence_data data
;
348 if (copy_from_user(&data
, (void __user
*)arg
, sizeof(data
))) {
353 pt
= sync_pt_create(obj
, data
.value
);
359 sync_file
= sync_file_create(&pt
->base
);
360 dma_fence_put(&pt
->base
);
367 if (copy_to_user((void __user
*)arg
, &data
, sizeof(data
))) {
368 fput(sync_file
->file
);
373 fd_install(fd
, sync_file
->file
);
382 static long sw_sync_ioctl_inc(struct sync_timeline
*obj
, unsigned long arg
)
386 if (copy_from_user(&value
, (void __user
*)arg
, sizeof(value
)))
389 while (value
> INT_MAX
) {
390 sync_timeline_signal(obj
, INT_MAX
);
394 sync_timeline_signal(obj
, value
);
399 static long sw_sync_ioctl(struct file
*file
, unsigned int cmd
,
402 struct sync_timeline
*obj
= file
->private_data
;
405 case SW_SYNC_IOC_CREATE_FENCE
:
406 return sw_sync_ioctl_create_fence(obj
, arg
);
408 case SW_SYNC_IOC_INC
:
409 return sw_sync_ioctl_inc(obj
, arg
);
416 const struct file_operations sw_sync_debugfs_fops
= {
417 .open
= sw_sync_debugfs_open
,
418 .release
= sw_sync_debugfs_release
,
419 .unlocked_ioctl
= sw_sync_ioctl
,
420 .compat_ioctl
= sw_sync_ioctl
,