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