xref: /linux/drivers/dma-buf/sw_sync.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sync File validation framework
4  *
5  * Copyright (C) 2012 Google, Inc.
6  */
7 
8 #include <linux/file.h>
9 #include <linux/fs.h>
10 #include <linux/uaccess.h>
11 #include <linux/slab.h>
12 #include <linux/sync_file.h>
13 
14 #include "sync_debug.h"
15 
16 #define CREATE_TRACE_POINTS
17 #include "sync_trace.h"
18 
19 /*
20  * SW SYNC validation framework
21  *
22  * A sync object driver that uses a 32bit counter to coordinate
23  * synchronization.  Useful when there is no hardware primitive backing
24  * the synchronization.
25  *
26  * To start the framework just open:
27  *
28  * <debugfs>/sync/sw_sync
29  *
30  * That will create a sync timeline, all fences created under this timeline
31  * file descriptor will belong to the this timeline.
32  *
33  * The 'sw_sync' file can be opened many times as to create different
34  * timelines.
35  *
36  * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
37  * sw_sync_create_fence_data as parameter.
38  *
39  * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
40  * with the increment as u32. This will update the last signaled value
41  * from the timeline and signal any fence that has a seqno smaller or equal
42  * to it.
43  *
44  * struct sw_sync_create_fence_data
45  * @value:	the seqno to initialise the fence with
46  * @name:	the name of the new sync point
47  * @fence:	return the fd of the new sync_file with the created fence
48  */
49 struct sw_sync_create_fence_data {
50 	__u32	value;
51 	char	name[32];
52 	__s32	fence; /* fd of new fence */
53 };
54 
55 /**
56  * struct sw_sync_get_deadline - get the deadline hint of a sw_sync fence
57  * @deadline_ns: absolute time of the deadline
58  * @pad:	must be zero
59  * @fence_fd:	the sw_sync fence fd (in)
60  *
61  * Return the earliest deadline set on the fence.  The timebase for the
62  * deadline is CLOCK_MONOTONIC (same as vblank).  If there is no deadline
63  * set on the fence, this ioctl will return -ENOENT.
64  */
65 struct sw_sync_get_deadline {
66 	__u64	deadline_ns;
67 	__u32	pad;
68 	__s32	fence_fd;
69 };
70 
71 #define SW_SYNC_IOC_MAGIC	'W'
72 
73 #define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
74 		struct sw_sync_create_fence_data)
75 
76 #define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
77 #define SW_SYNC_GET_DEADLINE		_IOWR(SW_SYNC_IOC_MAGIC, 2, \
78 		struct sw_sync_get_deadline)
79 
80 
81 #define SW_SYNC_HAS_DEADLINE_BIT	DMA_FENCE_FLAG_USER_BITS
82 
83 static const struct dma_fence_ops timeline_fence_ops;
84 
dma_fence_to_sync_pt(struct dma_fence * fence)85 static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
86 {
87 	if (fence->ops != &timeline_fence_ops)
88 		return NULL;
89 	return container_of(fence, struct sync_pt, base);
90 }
91 
92 /**
93  * sync_timeline_create() - creates a sync object
94  * @name:	sync_timeline name
95  *
96  * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
97  * case of error.
98  */
sync_timeline_create(const char * name)99 static struct sync_timeline *sync_timeline_create(const char *name)
100 {
101 	struct sync_timeline *obj;
102 
103 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
104 	if (!obj)
105 		return NULL;
106 
107 	kref_init(&obj->kref);
108 	obj->context = dma_fence_context_alloc(1);
109 	strscpy(obj->name, name, sizeof(obj->name));
110 
111 	obj->pt_tree = RB_ROOT;
112 	INIT_LIST_HEAD(&obj->pt_list);
113 	spin_lock_init(&obj->lock);
114 
115 	sync_timeline_debug_add(obj);
116 
117 	return obj;
118 }
119 
sync_timeline_free(struct kref * kref)120 static void sync_timeline_free(struct kref *kref)
121 {
122 	struct sync_timeline *obj =
123 		container_of(kref, struct sync_timeline, kref);
124 
125 	sync_timeline_debug_remove(obj);
126 
127 	kfree(obj);
128 }
129 
sync_timeline_get(struct sync_timeline * obj)130 static void sync_timeline_get(struct sync_timeline *obj)
131 {
132 	kref_get(&obj->kref);
133 }
134 
sync_timeline_put(struct sync_timeline * obj)135 static void sync_timeline_put(struct sync_timeline *obj)
136 {
137 	kref_put(&obj->kref, sync_timeline_free);
138 }
139 
timeline_fence_get_driver_name(struct dma_fence * fence)140 static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
141 {
142 	return "sw_sync";
143 }
144 
timeline_fence_get_timeline_name(struct dma_fence * fence)145 static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
146 {
147 	struct sync_timeline *parent = dma_fence_parent(fence);
148 
149 	return parent->name;
150 }
151 
timeline_fence_release(struct dma_fence * fence)152 static void timeline_fence_release(struct dma_fence *fence)
153 {
154 	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
155 	struct sync_timeline *parent = dma_fence_parent(fence);
156 	unsigned long flags;
157 
158 	spin_lock_irqsave(fence->lock, flags);
159 	if (!list_empty(&pt->link)) {
160 		list_del(&pt->link);
161 		rb_erase(&pt->node, &parent->pt_tree);
162 	}
163 	spin_unlock_irqrestore(fence->lock, flags);
164 
165 	sync_timeline_put(parent);
166 	dma_fence_free(fence);
167 }
168 
timeline_fence_signaled(struct dma_fence * fence)169 static bool timeline_fence_signaled(struct dma_fence *fence)
170 {
171 	struct sync_timeline *parent = dma_fence_parent(fence);
172 
173 	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
174 }
175 
timeline_fence_set_deadline(struct dma_fence * fence,ktime_t deadline)176 static void timeline_fence_set_deadline(struct dma_fence *fence, ktime_t deadline)
177 {
178 	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
179 	unsigned long flags;
180 
181 	spin_lock_irqsave(fence->lock, flags);
182 	if (test_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags)) {
183 		if (ktime_before(deadline, pt->deadline))
184 			pt->deadline = deadline;
185 	} else {
186 		pt->deadline = deadline;
187 		__set_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags);
188 	}
189 	spin_unlock_irqrestore(fence->lock, flags);
190 }
191 
192 static const struct dma_fence_ops timeline_fence_ops = {
193 	.get_driver_name = timeline_fence_get_driver_name,
194 	.get_timeline_name = timeline_fence_get_timeline_name,
195 	.signaled = timeline_fence_signaled,
196 	.release = timeline_fence_release,
197 	.set_deadline = timeline_fence_set_deadline,
198 };
199 
200 /**
201  * sync_timeline_signal() - signal a status change on a sync_timeline
202  * @obj:	sync_timeline to signal
203  * @inc:	num to increment on timeline->value
204  *
205  * A sync implementation should call this any time one of it's fences
206  * has signaled or has an error condition.
207  */
sync_timeline_signal(struct sync_timeline * obj,unsigned int inc)208 static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
209 {
210 	LIST_HEAD(signalled);
211 	struct sync_pt *pt, *next;
212 
213 	trace_sync_timeline(obj);
214 
215 	spin_lock_irq(&obj->lock);
216 
217 	obj->value += inc;
218 
219 	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
220 		if (!timeline_fence_signaled(&pt->base))
221 			break;
222 
223 		dma_fence_get(&pt->base);
224 
225 		list_move_tail(&pt->link, &signalled);
226 		rb_erase(&pt->node, &obj->pt_tree);
227 
228 		dma_fence_signal_locked(&pt->base);
229 	}
230 
231 	spin_unlock_irq(&obj->lock);
232 
233 	list_for_each_entry_safe(pt, next, &signalled, link) {
234 		list_del_init(&pt->link);
235 		dma_fence_put(&pt->base);
236 	}
237 }
238 
239 /**
240  * sync_pt_create() - creates a sync pt
241  * @obj:	parent sync_timeline
242  * @value:	value of the fence
243  *
244  * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
245  * allocated allowing for implementation specific data to be kept after
246  * the generic sync_timeline struct. Returns the sync_pt object or
247  * NULL in case of error.
248  */
sync_pt_create(struct sync_timeline * obj,unsigned int value)249 static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
250 				      unsigned int value)
251 {
252 	struct sync_pt *pt;
253 
254 	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
255 	if (!pt)
256 		return NULL;
257 
258 	sync_timeline_get(obj);
259 	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
260 		       obj->context, value);
261 	INIT_LIST_HEAD(&pt->link);
262 
263 	spin_lock_irq(&obj->lock);
264 	if (!dma_fence_is_signaled_locked(&pt->base)) {
265 		struct rb_node **p = &obj->pt_tree.rb_node;
266 		struct rb_node *parent = NULL;
267 
268 		while (*p) {
269 			struct sync_pt *other;
270 			int cmp;
271 
272 			parent = *p;
273 			other = rb_entry(parent, typeof(*pt), node);
274 			cmp = value - other->base.seqno;
275 			if (cmp > 0) {
276 				p = &parent->rb_right;
277 			} else if (cmp < 0) {
278 				p = &parent->rb_left;
279 			} else {
280 				if (dma_fence_get_rcu(&other->base)) {
281 					sync_timeline_put(obj);
282 					kfree(pt);
283 					pt = other;
284 					goto unlock;
285 				}
286 				p = &parent->rb_left;
287 			}
288 		}
289 		rb_link_node(&pt->node, parent, p);
290 		rb_insert_color(&pt->node, &obj->pt_tree);
291 
292 		parent = rb_next(&pt->node);
293 		list_add_tail(&pt->link,
294 			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
295 	}
296 unlock:
297 	spin_unlock_irq(&obj->lock);
298 
299 	return pt;
300 }
301 
302 /*
303  * *WARNING*
304  *
305  * improper use of this can result in deadlocking kernel drivers from userspace.
306  */
307 
308 /* opening sw_sync create a new sync obj */
sw_sync_debugfs_open(struct inode * inode,struct file * file)309 static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
310 {
311 	struct sync_timeline *obj;
312 	char task_comm[TASK_COMM_LEN];
313 
314 	get_task_comm(task_comm, current);
315 
316 	obj = sync_timeline_create(task_comm);
317 	if (!obj)
318 		return -ENOMEM;
319 
320 	file->private_data = obj;
321 
322 	return 0;
323 }
324 
sw_sync_debugfs_release(struct inode * inode,struct file * file)325 static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
326 {
327 	struct sync_timeline *obj = file->private_data;
328 	struct sync_pt *pt, *next;
329 
330 	spin_lock_irq(&obj->lock);
331 
332 	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
333 		dma_fence_set_error(&pt->base, -ENOENT);
334 		dma_fence_signal_locked(&pt->base);
335 	}
336 
337 	spin_unlock_irq(&obj->lock);
338 
339 	sync_timeline_put(obj);
340 	return 0;
341 }
342 
sw_sync_ioctl_create_fence(struct sync_timeline * obj,unsigned long arg)343 static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
344 				       unsigned long arg)
345 {
346 	int fd = get_unused_fd_flags(O_CLOEXEC);
347 	int err;
348 	struct sync_pt *pt;
349 	struct sync_file *sync_file;
350 	struct sw_sync_create_fence_data data;
351 
352 	if (fd < 0)
353 		return fd;
354 
355 	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
356 		err = -EFAULT;
357 		goto err;
358 	}
359 
360 	pt = sync_pt_create(obj, data.value);
361 	if (!pt) {
362 		err = -ENOMEM;
363 		goto err;
364 	}
365 
366 	sync_file = sync_file_create(&pt->base);
367 	dma_fence_put(&pt->base);
368 	if (!sync_file) {
369 		err = -ENOMEM;
370 		goto err;
371 	}
372 
373 	data.fence = fd;
374 	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
375 		fput(sync_file->file);
376 		err = -EFAULT;
377 		goto err;
378 	}
379 
380 	fd_install(fd, sync_file->file);
381 
382 	return 0;
383 
384 err:
385 	put_unused_fd(fd);
386 	return err;
387 }
388 
sw_sync_ioctl_inc(struct sync_timeline * obj,unsigned long arg)389 static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
390 {
391 	u32 value;
392 
393 	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
394 		return -EFAULT;
395 
396 	while (value > INT_MAX)  {
397 		sync_timeline_signal(obj, INT_MAX);
398 		value -= INT_MAX;
399 	}
400 
401 	sync_timeline_signal(obj, value);
402 
403 	return 0;
404 }
405 
sw_sync_ioctl_get_deadline(struct sync_timeline * obj,unsigned long arg)406 static int sw_sync_ioctl_get_deadline(struct sync_timeline *obj, unsigned long arg)
407 {
408 	struct sw_sync_get_deadline data;
409 	struct dma_fence *fence;
410 	unsigned long flags;
411 	struct sync_pt *pt;
412 	int ret = 0;
413 
414 	if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
415 		return -EFAULT;
416 
417 	if (data.deadline_ns || data.pad)
418 		return -EINVAL;
419 
420 	fence = sync_file_get_fence(data.fence_fd);
421 	if (!fence)
422 		return -EINVAL;
423 
424 	pt = dma_fence_to_sync_pt(fence);
425 	if (!pt) {
426 		ret = -EINVAL;
427 		goto put_fence;
428 	}
429 
430 	spin_lock_irqsave(fence->lock, flags);
431 	if (!test_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags)) {
432 		ret = -ENOENT;
433 		goto unlock;
434 	}
435 	data.deadline_ns = ktime_to_ns(pt->deadline);
436 	spin_unlock_irqrestore(fence->lock, flags);
437 
438 	dma_fence_put(fence);
439 
440 	if (ret)
441 		return ret;
442 
443 	if (copy_to_user((void __user *)arg, &data, sizeof(data)))
444 		return -EFAULT;
445 
446 	return 0;
447 
448 unlock:
449 	spin_unlock_irqrestore(fence->lock, flags);
450 put_fence:
451 	dma_fence_put(fence);
452 
453 	return ret;
454 }
455 
sw_sync_ioctl(struct file * file,unsigned int cmd,unsigned long arg)456 static long sw_sync_ioctl(struct file *file, unsigned int cmd,
457 			  unsigned long arg)
458 {
459 	struct sync_timeline *obj = file->private_data;
460 
461 	switch (cmd) {
462 	case SW_SYNC_IOC_CREATE_FENCE:
463 		return sw_sync_ioctl_create_fence(obj, arg);
464 
465 	case SW_SYNC_IOC_INC:
466 		return sw_sync_ioctl_inc(obj, arg);
467 
468 	case SW_SYNC_GET_DEADLINE:
469 		return sw_sync_ioctl_get_deadline(obj, arg);
470 
471 	default:
472 		return -ENOTTY;
473 	}
474 }
475 
476 const struct file_operations sw_sync_debugfs_fops = {
477 	.open           = sw_sync_debugfs_open,
478 	.release        = sw_sync_debugfs_release,
479 	.unlocked_ioctl = sw_sync_ioctl,
480 	.compat_ioctl	= compat_ptr_ioctl,
481 };
482