Blame view

drivers/dma-buf/sw_sync.c 9.46 KB
9c92ab619   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
1867a23b1   Gustavo Padovan   staging/android: ...
2
  /*
e912c881f   Gustavo Padovan   staging/android: ...
3
   * Sync File validation framework
1867a23b1   Gustavo Padovan   staging/android: ...
4
5
   *
   * Copyright (C) 2012 Google, Inc.
1867a23b1   Gustavo Padovan   staging/android: ...
6
7
8
9
10
   */
  
  #include <linux/file.h>
  #include <linux/fs.h>
  #include <linux/uaccess.h>
aff9da10e   Gustavo Padovan   staging/android: ...
11
  #include <linux/slab.h>
1867a23b1   Gustavo Padovan   staging/android: ...
12
  #include <linux/sync_file.h>
1fe82e2e1   Gustavo Padovan   staging/android: ...
13
  #include "sync_debug.h"
1867a23b1   Gustavo Padovan   staging/android: ...
14

aff9da10e   Gustavo Padovan   staging/android: ...
15
  #define CREATE_TRACE_POINTS
a04f915eb   Gustavo Padovan   staging/android: ...
16
  #include "sync_trace.h"
aff9da10e   Gustavo Padovan   staging/android: ...
17

fc0c9a03b   Gustavo Padovan   staging/android: ...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  /*
   * SW SYNC validation framework
   *
   * A sync object driver that uses a 32bit counter to coordinate
   * synchronization.  Useful when there is no hardware primitive backing
   * the synchronization.
   *
   * To start the framework just open:
   *
   * <debugfs>/sync/sw_sync
   *
   * That will create a sync timeline, all fences created under this timeline
   * file descriptor will belong to the this timeline.
   *
   * The 'sw_sync' file can be opened many times as to create different
   * timelines.
   *
   * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
e56a0fbd5   Shawn Guo   dma-buf/sw_sync: ...
36
   * sw_sync_create_fence_data as parameter.
fc0c9a03b   Gustavo Padovan   staging/android: ...
37
38
39
40
41
42
   *
   * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
   * with the increment as u32. This will update the last signaled value
   * from the timeline and signal any fence that has a seqno smaller or equal
   * to it.
   *
e56a0fbd5   Shawn Guo   dma-buf/sw_sync: ...
43
   * struct sw_sync_create_fence_data
fc0c9a03b   Gustavo Padovan   staging/android: ...
44
45
46
47
   * @value:	the seqno to initialise the fence with
   * @name:	the name of the new sync point
   * @fence:	return the fd of the new sync_file with the created fence
   */
6f65aa892   Gustavo Padovan   staging/android: ...
48
49
50
51
52
53
54
55
56
57
  struct sw_sync_create_fence_data {
  	__u32	value;
  	char	name[32];
  	__s32	fence; /* fd of new fence */
  };
  
  #define SW_SYNC_IOC_MAGIC	'W'
  
  #define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
  		struct sw_sync_create_fence_data)
fc0c9a03b   Gustavo Padovan   staging/android: ...
58

6f65aa892   Gustavo Padovan   staging/android: ...
59
  #define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
f54d18670   Chris Wilson   dma-buf: Rename s...
60
  static const struct dma_fence_ops timeline_fence_ops;
aff9da10e   Gustavo Padovan   staging/android: ...
61

f54d18670   Chris Wilson   dma-buf: Rename s...
62
  static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
aff9da10e   Gustavo Padovan   staging/android: ...
63
64
65
66
67
68
69
70
  {
  	if (fence->ops != &timeline_fence_ops)
  		return NULL;
  	return container_of(fence, struct sync_pt, base);
  }
  
  /**
   * sync_timeline_create() - creates a sync object
aff9da10e   Gustavo Padovan   staging/android: ...
71
72
73
74
75
   * @name:	sync_timeline name
   *
   * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
   * case of error.
   */
748815881   Baoyou Xie   dma-buf/sw_sync: ...
76
  static struct sync_timeline *sync_timeline_create(const char *name)
aff9da10e   Gustavo Padovan   staging/android: ...
77
78
79
80
81
82
83
84
  {
  	struct sync_timeline *obj;
  
  	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  	if (!obj)
  		return NULL;
  
  	kref_init(&obj->kref);
f54d18670   Chris Wilson   dma-buf: Rename s...
85
  	obj->context = dma_fence_context_alloc(1);
aff9da10e   Gustavo Padovan   staging/android: ...
86
  	strlcpy(obj->name, name, sizeof(obj->name));
aff9da10e   Gustavo Padovan   staging/android: ...
87

f1e8c6712   Chris Wilson   dma-buf/sw-sync: ...
88
  	obj->pt_tree = RB_ROOT;
d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
89
90
  	INIT_LIST_HEAD(&obj->pt_list);
  	spin_lock_init(&obj->lock);
aff9da10e   Gustavo Padovan   staging/android: ...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  
  	sync_timeline_debug_add(obj);
  
  	return obj;
  }
  
  static void sync_timeline_free(struct kref *kref)
  {
  	struct sync_timeline *obj =
  		container_of(kref, struct sync_timeline, kref);
  
  	sync_timeline_debug_remove(obj);
  
  	kfree(obj);
  }
  
  static void sync_timeline_get(struct sync_timeline *obj)
  {
  	kref_get(&obj->kref);
  }
  
  static void sync_timeline_put(struct sync_timeline *obj)
  {
  	kref_put(&obj->kref, sync_timeline_free);
  }
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
  {
  	return "sw_sync";
  }
  
  static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
  {
  	struct sync_timeline *parent = dma_fence_parent(fence);
  
  	return parent->name;
  }
  
  static void timeline_fence_release(struct dma_fence *fence)
  {
  	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
  	struct sync_timeline *parent = dma_fence_parent(fence);
d3c6dd1fb   Chris Wilson   dma-buf/sw_sync: ...
132
  	unsigned long flags;
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
133

d3c6dd1fb   Chris Wilson   dma-buf/sw_sync: ...
134
  	spin_lock_irqsave(fence->lock, flags);
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
135
  	if (!list_empty(&pt->link)) {
d3c6dd1fb   Chris Wilson   dma-buf/sw_sync: ...
136
137
  		list_del(&pt->link);
  		rb_erase(&pt->node, &parent->pt_tree);
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
138
  	}
d3c6dd1fb   Chris Wilson   dma-buf/sw_sync: ...
139
  	spin_unlock_irqrestore(fence->lock, flags);
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
140
141
142
143
144
145
146
147
  
  	sync_timeline_put(parent);
  	dma_fence_free(fence);
  }
  
  static bool timeline_fence_signaled(struct dma_fence *fence)
  {
  	struct sync_timeline *parent = dma_fence_parent(fence);
5e498abf1   Christian König   dma-buf: explicit...
148
  	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
149
150
151
152
153
154
155
156
157
158
  }
  
  static bool timeline_fence_enable_signaling(struct dma_fence *fence)
  {
  	return true;
  }
  
  static void timeline_fence_value_str(struct dma_fence *fence,
  				    char *str, int size)
  {
b312d8ca3   Christian König   dma-buf: make fen...
159
  	snprintf(str, size, "%lld", fence->seqno);
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  }
  
  static void timeline_fence_timeline_value_str(struct dma_fence *fence,
  					     char *str, int size)
  {
  	struct sync_timeline *parent = dma_fence_parent(fence);
  
  	snprintf(str, size, "%d", parent->value);
  }
  
  static const struct dma_fence_ops timeline_fence_ops = {
  	.get_driver_name = timeline_fence_get_driver_name,
  	.get_timeline_name = timeline_fence_get_timeline_name,
  	.enable_signaling = timeline_fence_enable_signaling,
  	.signaled = timeline_fence_signaled,
150b6a9d7   Gustavo Padovan   dma-buf/sw_sync: ...
175
176
177
178
  	.release = timeline_fence_release,
  	.fence_value_str = timeline_fence_value_str,
  	.timeline_value_str = timeline_fence_timeline_value_str,
  };
aff9da10e   Gustavo Padovan   staging/android: ...
179
  /**
aff9da10e   Gustavo Padovan   staging/android: ...
180
181
182
183
184
185
186
187
188
   * sync_timeline_signal() - signal a status change on a sync_timeline
   * @obj:	sync_timeline to signal
   * @inc:	num to increment on timeline->value
   *
   * A sync implementation should call this any time one of it's fences
   * has signaled or has an error condition.
   */
  static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  {
aff9da10e   Gustavo Padovan   staging/android: ...
189
190
191
  	struct sync_pt *pt, *next;
  
  	trace_sync_timeline(obj);
d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
192
  	spin_lock_irq(&obj->lock);
aff9da10e   Gustavo Padovan   staging/android: ...
193
194
  
  	obj->value += inc;
f1e8c6712   Chris Wilson   dma-buf/sw-sync: ...
195
  	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
3792b7c1a   Gustavo Padovan   dma-buf/sw_sync: ...
196
  		if (!timeline_fence_signaled(&pt->base))
f1e8c6712   Chris Wilson   dma-buf/sw-sync: ...
197
198
199
200
  			break;
  
  		list_del_init(&pt->link);
  		rb_erase(&pt->node, &obj->pt_tree);
3792b7c1a   Gustavo Padovan   dma-buf/sw_sync: ...
201
202
203
204
205
206
207
208
209
210
  
  		/*
  		 * A signal callback may release the last reference to this
  		 * fence, causing it to be freed. That operation has to be
  		 * last to avoid a use after free inside this loop, and must
  		 * be after we remove the fence from the timeline in order to
  		 * prevent deadlocking on timeline->lock inside
  		 * timeline_fence_release().
  		 */
  		dma_fence_signal_locked(&pt->base);
f1e8c6712   Chris Wilson   dma-buf/sw-sync: ...
211
  	}
aff9da10e   Gustavo Padovan   staging/android: ...
212

d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
213
  	spin_unlock_irq(&obj->lock);
aff9da10e   Gustavo Padovan   staging/android: ...
214
215
216
217
  }
  
  /**
   * sync_pt_create() - creates a sync pt
b88132b4a   Chris Wilson   dma-buf/sw_sync: ...
218
219
   * @obj:	parent sync_timeline
   * @value:	value of the fence
aff9da10e   Gustavo Padovan   staging/android: ...
220
   *
b88132b4a   Chris Wilson   dma-buf/sw_sync: ...
221
   * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
aff9da10e   Gustavo Padovan   staging/android: ...
222
223
224
225
   * allocated allowing for implementation specific data to be kept after
   * the generic sync_timeline struct. Returns the sync_pt object or
   * NULL in case of error.
   */
3b52ce44e   Chris Wilson   dma-buf/sw-sync: ...
226
227
  static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
  				      unsigned int value)
aff9da10e   Gustavo Padovan   staging/android: ...
228
  {
aff9da10e   Gustavo Padovan   staging/android: ...
229
  	struct sync_pt *pt;
3b52ce44e   Chris Wilson   dma-buf/sw-sync: ...
230
  	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
aff9da10e   Gustavo Padovan   staging/android: ...
231
232
  	if (!pt)
  		return NULL;
aff9da10e   Gustavo Padovan   staging/android: ...
233
  	sync_timeline_get(obj);
d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
234
  	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
f54d18670   Chris Wilson   dma-buf: Rename s...
235
  		       obj->context, value);
d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
236
  	INIT_LIST_HEAD(&pt->link);
a6aa8fca4   Chris Wilson   dma-buf/sw-sync: ...
237

d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
238
  	spin_lock_irq(&obj->lock);
f1e8c6712   Chris Wilson   dma-buf/sw-sync: ...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  	if (!dma_fence_is_signaled_locked(&pt->base)) {
  		struct rb_node **p = &obj->pt_tree.rb_node;
  		struct rb_node *parent = NULL;
  
  		while (*p) {
  			struct sync_pt *other;
  			int cmp;
  
  			parent = *p;
  			other = rb_entry(parent, typeof(*pt), node);
  			cmp = value - other->base.seqno;
  			if (cmp > 0) {
  				p = &parent->rb_right;
  			} else if (cmp < 0) {
  				p = &parent->rb_left;
  			} else {
  				if (dma_fence_get_rcu(&other->base)) {
d3c6dd1fb   Chris Wilson   dma-buf/sw_sync: ...
256
257
  					sync_timeline_put(obj);
  					kfree(pt);
f1e8c6712   Chris Wilson   dma-buf/sw-sync: ...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  					pt = other;
  					goto unlock;
  				}
  				p = &parent->rb_left;
  			}
  		}
  		rb_link_node(&pt->node, parent, p);
  		rb_insert_color(&pt->node, &obj->pt_tree);
  
  		parent = rb_next(&pt->node);
  		list_add_tail(&pt->link,
  			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
  	}
  unlock:
d3862e44d   Chris Wilson   dma-buf/sw-sync: ...
272
  	spin_unlock_irq(&obj->lock);
a6aa8fca4   Chris Wilson   dma-buf/sw-sync: ...
273

aff9da10e   Gustavo Padovan   staging/android: ...
274
275
  	return pt;
  }
1867a23b1   Gustavo Padovan   staging/android: ...
276
277
278
279
280
281
282
283
284
285
286
287
288
  /*
   * *WARNING*
   *
   * improper use of this can result in deadlocking kernel drivers from userspace.
   */
  
  /* opening sw_sync create a new sync obj */
  static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
  {
  	struct sync_timeline *obj;
  	char task_comm[TASK_COMM_LEN];
  
  	get_task_comm(task_comm, current);
b9bc2b7b6   Gustavo Padovan   staging/android: ...
289
  	obj = sync_timeline_create(task_comm);
1867a23b1   Gustavo Padovan   staging/android: ...
290
291
292
293
294
295
296
297
298
299
300
  	if (!obj)
  		return -ENOMEM;
  
  	file->private_data = obj;
  
  	return 0;
  }
  
  static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
  {
  	struct sync_timeline *obj = file->private_data;
ea4d5a270   Dominik Behr   dma-buf/sw_sync: ...
301
302
303
304
305
306
307
308
  	struct sync_pt *pt, *next;
  
  	spin_lock_irq(&obj->lock);
  
  	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
  		dma_fence_set_error(&pt->base, -ENOENT);
  		dma_fence_signal_locked(&pt->base);
  	}
1867a23b1   Gustavo Padovan   staging/android: ...
309

ea4d5a270   Dominik Behr   dma-buf/sw_sync: ...
310
  	spin_unlock_irq(&obj->lock);
711102325   Gustavo Padovan   staging/android: ...
311
312
  
  	sync_timeline_put(obj);
1867a23b1   Gustavo Padovan   staging/android: ...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  	return 0;
  }
  
  static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
  				       unsigned long arg)
  {
  	int fd = get_unused_fd_flags(O_CLOEXEC);
  	int err;
  	struct sync_pt *pt;
  	struct sync_file *sync_file;
  	struct sw_sync_create_fence_data data;
  
  	if (fd < 0)
  		return fd;
  
  	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  		err = -EFAULT;
  		goto err;
  	}
3b52ce44e   Chris Wilson   dma-buf/sw-sync: ...
332
  	pt = sync_pt_create(obj, data.value);
1867a23b1   Gustavo Padovan   staging/android: ...
333
334
335
336
337
338
  	if (!pt) {
  		err = -ENOMEM;
  		goto err;
  	}
  
  	sync_file = sync_file_create(&pt->base);
4592bfcd1   Gustavo Padovan   dma-buf/sw_sync: ...
339
  	dma_fence_put(&pt->base);
1867a23b1   Gustavo Padovan   staging/android: ...
340
  	if (!sync_file) {
1867a23b1   Gustavo Padovan   staging/android: ...
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  		err = -ENOMEM;
  		goto err;
  	}
  
  	data.fence = fd;
  	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  		fput(sync_file->file);
  		err = -EFAULT;
  		goto err;
  	}
  
  	fd_install(fd, sync_file->file);
  
  	return 0;
  
  err:
  	put_unused_fd(fd);
  	return err;
  }
  
  static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
  {
  	u32 value;
  
  	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
  		return -EFAULT;
8f66d3aa1   Chris Wilson   dma-buf/sw-sync: ...
367
368
369
370
  	while (value > INT_MAX)  {
  		sync_timeline_signal(obj, INT_MAX);
  		value -= INT_MAX;
  	}
1867a23b1   Gustavo Padovan   staging/android: ...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  	sync_timeline_signal(obj, value);
  
  	return 0;
  }
  
  static long sw_sync_ioctl(struct file *file, unsigned int cmd,
  			  unsigned long arg)
  {
  	struct sync_timeline *obj = file->private_data;
  
  	switch (cmd) {
  	case SW_SYNC_IOC_CREATE_FENCE:
  		return sw_sync_ioctl_create_fence(obj, arg);
  
  	case SW_SYNC_IOC_INC:
  		return sw_sync_ioctl_inc(obj, arg);
  
  	default:
  		return -ENOTTY;
  	}
  }
  
  const struct file_operations sw_sync_debugfs_fops = {
  	.open           = sw_sync_debugfs_open,
  	.release        = sw_sync_debugfs_release,
  	.unlocked_ioctl = sw_sync_ioctl,
1832f2d8f   Arnd Bergmann   compat_ioctl: mov...
397
  	.compat_ioctl	= compat_ptr_ioctl,
1867a23b1   Gustavo Padovan   staging/android: ...
398
  };