Commit | Line | Data |
---|---|---|
0f0d8406 ML |
1 | /* |
2 | * drivers/base/sync.c | |
3 | * | |
4 | * Copyright (C) 2012 Google, Inc. | |
5 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
15 | */ | |
16 | ||
17 | #include <linux/debugfs.h> | |
18 | #include <linux/export.h> | |
19 | #include <linux/file.h> | |
20 | #include <linux/fs.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/poll.h> | |
23 | #include <linux/sched.h> | |
24 | #include <linux/seq_file.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/uaccess.h> | |
27 | #include <linux/anon_inodes.h> | |
353fdf17 | 28 | #include <linux/time64.h> |
a44eb74c | 29 | #include "sw_sync.h" |
0f0d8406 ML |
30 | |
31 | #ifdef CONFIG_DEBUG_FS | |
32 | ||
8a004484 GP |
33 | static struct dentry *dbgfs; |
34 | ||
0f0d8406 ML |
35 | static LIST_HEAD(sync_timeline_list_head); |
36 | static DEFINE_SPINLOCK(sync_timeline_list_lock); | |
d7fdb0ae GP |
37 | static LIST_HEAD(sync_file_list_head); |
38 | static DEFINE_SPINLOCK(sync_file_list_lock); | |
0f0d8406 ML |
39 | |
40 | void sync_timeline_debug_add(struct sync_timeline *obj) | |
41 | { | |
42 | unsigned long flags; | |
43 | ||
44 | spin_lock_irqsave(&sync_timeline_list_lock, flags); | |
45 | list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head); | |
46 | spin_unlock_irqrestore(&sync_timeline_list_lock, flags); | |
47 | } | |
48 | ||
49 | void sync_timeline_debug_remove(struct sync_timeline *obj) | |
50 | { | |
51 | unsigned long flags; | |
52 | ||
53 | spin_lock_irqsave(&sync_timeline_list_lock, flags); | |
54 | list_del(&obj->sync_timeline_list); | |
55 | spin_unlock_irqrestore(&sync_timeline_list_lock, flags); | |
56 | } | |
57 | ||
d7fdb0ae | 58 | void sync_file_debug_add(struct sync_file *sync_file) |
0f0d8406 ML |
59 | { |
60 | unsigned long flags; | |
61 | ||
d7fdb0ae GP |
62 | spin_lock_irqsave(&sync_file_list_lock, flags); |
63 | list_add_tail(&sync_file->sync_file_list, &sync_file_list_head); | |
64 | spin_unlock_irqrestore(&sync_file_list_lock, flags); | |
0f0d8406 ML |
65 | } |
66 | ||
d7fdb0ae | 67 | void sync_file_debug_remove(struct sync_file *sync_file) |
0f0d8406 ML |
68 | { |
69 | unsigned long flags; | |
70 | ||
d7fdb0ae GP |
71 | spin_lock_irqsave(&sync_file_list_lock, flags); |
72 | list_del(&sync_file->sync_file_list); | |
73 | spin_unlock_irqrestore(&sync_file_list_lock, flags); | |
0f0d8406 ML |
74 | } |
75 | ||
76 | static const char *sync_status_str(int status) | |
77 | { | |
78 | if (status == 0) | |
79 | return "signaled"; | |
95451355 PST |
80 | |
81 | if (status > 0) | |
0f0d8406 | 82 | return "active"; |
95451355 PST |
83 | |
84 | return "error"; | |
0f0d8406 ML |
85 | } |
86 | ||
b55b54b5 | 87 | static void sync_print_fence(struct seq_file *s, struct fence *fence, bool show) |
0f0d8406 ML |
88 | { |
89 | int status = 1; | |
b55b54b5 | 90 | struct sync_timeline *parent = fence_parent(fence); |
0f0d8406 | 91 | |
b55b54b5 GP |
92 | if (fence_is_signaled_locked(fence)) |
93 | status = fence->status; | |
0f0d8406 | 94 | |
b55b54b5 GP |
95 | seq_printf(s, " %s%sfence %s", |
96 | show ? parent->name : "", | |
97 | show ? "_" : "", | |
0f0d8406 ML |
98 | sync_status_str(status)); |
99 | ||
100 | if (status <= 0) { | |
0541cdf5 | 101 | struct timespec64 ts64 = |
b55b54b5 | 102 | ktime_to_timespec64(fence->timestamp); |
95451355 | 103 | |
353fdf17 | 104 | seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec); |
0f0d8406 ML |
105 | } |
106 | ||
b55b54b5 GP |
107 | if ((!fence || fence->ops->timeline_value_str) && |
108 | fence->ops->fence_value_str) { | |
0f0d8406 | 109 | char value[64]; |
73465f1c | 110 | bool success; |
95451355 | 111 | |
b55b54b5 | 112 | fence->ops->fence_value_str(fence, value, sizeof(value)); |
73465f1c ML |
113 | success = strlen(value); |
114 | ||
115 | if (success) | |
116 | seq_printf(s, ": %s", value); | |
117 | ||
118 | if (success && fence) { | |
b55b54b5 GP |
119 | fence->ops->timeline_value_str(fence, value, |
120 | sizeof(value)); | |
73465f1c ML |
121 | |
122 | if (strlen(value)) | |
123 | seq_printf(s, " / %s", value); | |
0f0d8406 ML |
124 | } |
125 | } | |
126 | ||
127 | seq_puts(s, "\n"); | |
128 | } | |
129 | ||
130 | static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj) | |
131 | { | |
132 | struct list_head *pos; | |
133 | unsigned long flags; | |
134 | ||
135 | seq_printf(s, "%s %s", obj->name, obj->ops->driver_name); | |
136 | ||
137 | if (obj->ops->timeline_value_str) { | |
138 | char value[64]; | |
95451355 | 139 | |
0f0d8406 ML |
140 | obj->ops->timeline_value_str(obj, value, sizeof(value)); |
141 | seq_printf(s, ": %s", value); | |
142 | } | |
143 | ||
144 | seq_puts(s, "\n"); | |
145 | ||
146 | spin_lock_irqsave(&obj->child_list_lock, flags); | |
147 | list_for_each(pos, &obj->child_list_head) { | |
b55b54b5 GP |
148 | struct fence *fence = |
149 | container_of(pos, struct fence, child_list); | |
150 | sync_print_fence(s, fence, false); | |
0f0d8406 ML |
151 | } |
152 | spin_unlock_irqrestore(&obj->child_list_lock, flags); | |
153 | } | |
154 | ||
d7fdb0ae GP |
155 | static void sync_print_sync_file(struct seq_file *s, |
156 | struct sync_file *sync_file) | |
b55b54b5 | 157 | { |
0f0d8406 ML |
158 | int i; |
159 | ||
d7fdb0ae GP |
160 | seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name, |
161 | sync_status_str(atomic_read(&sync_file->status))); | |
0f0d8406 | 162 | |
d7fdb0ae | 163 | for (i = 0; i < sync_file->num_fences; ++i) |
b55b54b5 GP |
164 | sync_print_fence(s, sync_file->cbs[i].fence, true); |
165 | } | |
166 | ||
0f0d8406 ML |
167 | static int sync_debugfs_show(struct seq_file *s, void *unused) |
168 | { | |
169 | unsigned long flags; | |
170 | struct list_head *pos; | |
171 | ||
172 | seq_puts(s, "objs:\n--------------\n"); | |
173 | ||
174 | spin_lock_irqsave(&sync_timeline_list_lock, flags); | |
175 | list_for_each(pos, &sync_timeline_list_head) { | |
176 | struct sync_timeline *obj = | |
177 | container_of(pos, struct sync_timeline, | |
178 | sync_timeline_list); | |
179 | ||
180 | sync_print_obj(s, obj); | |
181 | seq_puts(s, "\n"); | |
182 | } | |
183 | spin_unlock_irqrestore(&sync_timeline_list_lock, flags); | |
184 | ||
185 | seq_puts(s, "fences:\n--------------\n"); | |
186 | ||
d7fdb0ae GP |
187 | spin_lock_irqsave(&sync_file_list_lock, flags); |
188 | list_for_each(pos, &sync_file_list_head) { | |
189 | struct sync_file *sync_file = | |
190 | container_of(pos, struct sync_file, sync_file_list); | |
0f0d8406 | 191 | |
d7fdb0ae | 192 | sync_print_sync_file(s, sync_file); |
0f0d8406 ML |
193 | seq_puts(s, "\n"); |
194 | } | |
d7fdb0ae | 195 | spin_unlock_irqrestore(&sync_file_list_lock, flags); |
0f0d8406 ML |
196 | return 0; |
197 | } | |
198 | ||
8a004484 | 199 | static int sync_info_debugfs_open(struct inode *inode, struct file *file) |
0f0d8406 ML |
200 | { |
201 | return single_open(file, sync_debugfs_show, inode->i_private); | |
202 | } | |
203 | ||
8a004484 GP |
204 | static const struct file_operations sync_info_debugfs_fops = { |
205 | .open = sync_info_debugfs_open, | |
0f0d8406 ML |
206 | .read = seq_read, |
207 | .llseek = seq_lseek, | |
208 | .release = single_release, | |
209 | }; | |
210 | ||
a44eb74c GP |
211 | /* |
212 | * *WARNING* | |
213 | * | |
214 | * improper use of this can result in deadlocking kernel drivers from userspace. | |
215 | */ | |
216 | ||
217 | /* opening sw_sync create a new sync obj */ | |
218 | static int sw_sync_debugfs_open(struct inode *inode, struct file *file) | |
219 | { | |
220 | struct sw_sync_timeline *obj; | |
221 | char task_comm[TASK_COMM_LEN]; | |
222 | ||
223 | get_task_comm(task_comm, current); | |
224 | ||
225 | obj = sw_sync_timeline_create(task_comm); | |
226 | if (!obj) | |
227 | return -ENOMEM; | |
228 | ||
229 | file->private_data = obj; | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int sw_sync_debugfs_release(struct inode *inode, struct file *file) | |
235 | { | |
236 | struct sw_sync_timeline *obj = file->private_data; | |
237 | ||
238 | sync_timeline_destroy(&obj->obj); | |
239 | return 0; | |
240 | } | |
241 | ||
242 | static long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj, | |
243 | unsigned long arg) | |
244 | { | |
245 | int fd = get_unused_fd_flags(O_CLOEXEC); | |
246 | int err; | |
b55b54b5 | 247 | struct fence *fence; |
d7fdb0ae | 248 | struct sync_file *sync_file; |
a44eb74c GP |
249 | struct sw_sync_create_fence_data data; |
250 | ||
251 | if (fd < 0) | |
252 | return fd; | |
253 | ||
254 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) { | |
255 | err = -EFAULT; | |
256 | goto err; | |
257 | } | |
258 | ||
b55b54b5 GP |
259 | fence = sw_sync_pt_create(obj, data.value); |
260 | if (!fence) { | |
a44eb74c GP |
261 | err = -ENOMEM; |
262 | goto err; | |
263 | } | |
264 | ||
265 | data.name[sizeof(data.name) - 1] = '\0'; | |
b55b54b5 | 266 | sync_file = sync_file_create(data.name, fence); |
d7fdb0ae | 267 | if (!sync_file) { |
b55b54b5 | 268 | fence_put(fence); |
a44eb74c GP |
269 | err = -ENOMEM; |
270 | goto err; | |
271 | } | |
272 | ||
273 | data.fence = fd; | |
274 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { | |
d7fdb0ae | 275 | sync_file_put(sync_file); |
a44eb74c GP |
276 | err = -EFAULT; |
277 | goto err; | |
278 | } | |
279 | ||
d7fdb0ae | 280 | sync_file_install(sync_file, fd); |
a44eb74c GP |
281 | |
282 | return 0; | |
283 | ||
284 | err: | |
285 | put_unused_fd(fd); | |
286 | return err; | |
287 | } | |
288 | ||
289 | static long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg) | |
290 | { | |
291 | u32 value; | |
292 | ||
293 | if (copy_from_user(&value, (void __user *)arg, sizeof(value))) | |
294 | return -EFAULT; | |
295 | ||
296 | sw_sync_timeline_inc(obj, value); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | static long sw_sync_ioctl(struct file *file, unsigned int cmd, | |
302 | unsigned long arg) | |
303 | { | |
304 | struct sw_sync_timeline *obj = file->private_data; | |
305 | ||
306 | switch (cmd) { | |
307 | case SW_SYNC_IOC_CREATE_FENCE: | |
308 | return sw_sync_ioctl_create_fence(obj, arg); | |
309 | ||
310 | case SW_SYNC_IOC_INC: | |
311 | return sw_sync_ioctl_inc(obj, arg); | |
312 | ||
313 | default: | |
314 | return -ENOTTY; | |
315 | } | |
316 | } | |
317 | ||
318 | static const struct file_operations sw_sync_debugfs_fops = { | |
319 | .open = sw_sync_debugfs_open, | |
320 | .release = sw_sync_debugfs_release, | |
321 | .unlocked_ioctl = sw_sync_ioctl, | |
322 | .compat_ioctl = sw_sync_ioctl, | |
323 | }; | |
324 | ||
0f0d8406 ML |
325 | static __init int sync_debugfs_init(void) |
326 | { | |
8a004484 GP |
327 | dbgfs = debugfs_create_dir("sync", NULL); |
328 | ||
329 | debugfs_create_file("info", 0444, dbgfs, NULL, &sync_info_debugfs_fops); | |
a44eb74c GP |
330 | debugfs_create_file("sw_sync", 0644, dbgfs, NULL, |
331 | &sw_sync_debugfs_fops); | |
8a004484 | 332 | |
0f0d8406 ML |
333 | return 0; |
334 | } | |
335 | late_initcall(sync_debugfs_init); | |
336 | ||
337 | #define DUMP_CHUNK 256 | |
338 | static char sync_dump_buf[64 * 1024]; | |
339 | void sync_dump(void) | |
340 | { | |
341 | struct seq_file s = { | |
342 | .buf = sync_dump_buf, | |
343 | .size = sizeof(sync_dump_buf) - 1, | |
344 | }; | |
345 | int i; | |
346 | ||
347 | sync_debugfs_show(&s, NULL); | |
348 | ||
349 | for (i = 0; i < s.count; i += DUMP_CHUNK) { | |
350 | if ((s.count - i) > DUMP_CHUNK) { | |
351 | char c = s.buf[i + DUMP_CHUNK]; | |
95451355 | 352 | |
0f0d8406 ML |
353 | s.buf[i + DUMP_CHUNK] = 0; |
354 | pr_cont("%s", s.buf + i); | |
355 | s.buf[i + DUMP_CHUNK] = c; | |
356 | } else { | |
357 | s.buf[s.count] = 0; | |
358 | pr_cont("%s", s.buf + i); | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | #endif |