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/debugfs.h>
18 #include <linux/module.h>
19 #include <linux/export.h>
20 #include <linux/file.h>
22 #include <linux/kernel.h>
23 #include <linux/poll.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/time64.h>
32 #ifdef CONFIG_DEBUG_FS
34 static struct dentry *dbgfs;
36 static LIST_HEAD(sync_timeline_list_head);
37 static DEFINE_SPINLOCK(sync_timeline_list_lock);
38 static LIST_HEAD(sync_file_list_head);
39 static DEFINE_SPINLOCK(sync_file_list_lock);
41 void sync_timeline_debug_add(struct sync_timeline *obj)
45 spin_lock_irqsave(&sync_timeline_list_lock, flags);
46 list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
47 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
50 void sync_timeline_debug_remove(struct sync_timeline *obj)
54 spin_lock_irqsave(&sync_timeline_list_lock, flags);
55 list_del(&obj->sync_timeline_list);
56 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
59 void sync_file_debug_add(struct sync_file *sync_file)
63 spin_lock_irqsave(&sync_file_list_lock, flags);
64 list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
65 spin_unlock_irqrestore(&sync_file_list_lock, flags);
68 void sync_file_debug_remove(struct sync_file *sync_file)
72 spin_lock_irqsave(&sync_file_list_lock, flags);
73 list_del(&sync_file->sync_file_list);
74 spin_unlock_irqrestore(&sync_file_list_lock, flags);
77 static const char *sync_status_str(int status)
88 static void sync_print_pt(struct seq_file *s, struct fence *pt, bool fence)
92 if (fence_is_signaled_locked(pt))
95 seq_printf(s, " %s%spt %s",
96 fence && pt->ops->get_timeline_name ?
97 pt->ops->get_timeline_name(pt) : "",
99 sync_status_str(status));
102 struct timespec64 ts64 =
103 ktime_to_timespec64(pt->timestamp);
105 seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
108 if ((!fence || pt->ops->timeline_value_str) &&
109 pt->ops->fence_value_str) {
113 pt->ops->fence_value_str(pt, value, sizeof(value));
114 success = strlen(value);
117 seq_printf(s, ": %s", value);
119 if (success && fence) {
120 pt->ops->timeline_value_str(pt, value, sizeof(value));
123 seq_printf(s, " / %s", value);
130 static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
132 struct list_head *pos;
135 seq_printf(s, "%s %s", obj->name, obj->ops->driver_name);
137 if (obj->ops->timeline_value_str) {
140 obj->ops->timeline_value_str(obj, value, sizeof(value));
141 seq_printf(s, ": %s", value);
146 spin_lock_irqsave(&obj->child_list_lock, flags);
147 list_for_each(pos, &obj->child_list_head) {
149 container_of(pos, struct sync_pt, child_list);
150 sync_print_pt(s, &pt->base, false);
152 spin_unlock_irqrestore(&obj->child_list_lock, flags);
155 static void sync_print_sync_file(struct seq_file *s,
156 struct sync_file *sync_file)
160 seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name,
161 sync_status_str(atomic_read(&sync_file->status)));
163 for (i = 0; i < sync_file->num_fences; ++i)
164 sync_print_pt(s, sync_file->cbs[i].sync_pt, true);
167 static int sync_debugfs_show(struct seq_file *s, void *unused)
170 struct list_head *pos;
172 seq_puts(s, "objs:\n--------------\n");
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,
180 sync_print_obj(s, obj);
183 spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
185 seq_puts(s, "fences:\n--------------\n");
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);
192 sync_print_sync_file(s, sync_file);
195 spin_unlock_irqrestore(&sync_file_list_lock, flags);
199 static int sync_info_debugfs_open(struct inode *inode, struct file *file)
201 return single_open(file, sync_debugfs_show, inode->i_private);
204 static const struct file_operations sync_info_debugfs_fops = {
205 .open = sync_info_debugfs_open,
208 .release = single_release,
214 * improper use of this can result in deadlocking kernel drivers from userspace.
217 /* opening sw_sync create a new sync obj */
218 static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
220 struct sw_sync_timeline *obj;
221 char task_comm[TASK_COMM_LEN];
223 get_task_comm(task_comm, current);
225 obj = sw_sync_timeline_create(task_comm);
229 file->private_data = obj;
234 static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
236 struct sw_sync_timeline *obj = file->private_data;
238 sync_timeline_destroy(&obj->obj);
242 static long sw_sync_ioctl_create_fence(struct sw_sync_timeline *obj,
245 int fd = get_unused_fd_flags(O_CLOEXEC);
248 struct sync_file *sync_file;
249 struct sw_sync_create_fence_data data;
254 if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
259 pt = sw_sync_pt_create(obj, data.value);
265 data.name[sizeof(data.name) - 1] = '\0';
266 sync_file = sync_file_create(data.name, pt);
274 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
275 sync_file_put(sync_file);
280 sync_file_install(sync_file, fd);
289 static long sw_sync_ioctl_inc(struct sw_sync_timeline *obj, unsigned long arg)
293 if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
296 sw_sync_timeline_inc(obj, value);
301 static long sw_sync_ioctl(struct file *file, unsigned int cmd,
304 struct sw_sync_timeline *obj = file->private_data;
307 case SW_SYNC_IOC_CREATE_FENCE:
308 return sw_sync_ioctl_create_fence(obj, arg);
310 case SW_SYNC_IOC_INC:
311 return sw_sync_ioctl_inc(obj, arg);
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,
325 static __init int sync_debugfs_init(void)
327 dbgfs = debugfs_create_dir("sync", NULL);
329 debugfs_create_file("info", 0444, dbgfs, NULL, &sync_info_debugfs_fops);
330 debugfs_create_file("sw_sync", 0644, dbgfs, NULL,
331 &sw_sync_debugfs_fops);
335 late_initcall(sync_debugfs_init);
337 static __exit void sync_debugfs_exit(void)
340 debugfs_remove_recursive(dbgfs);
342 module_exit(sync_debugfs_exit);
344 #define DUMP_CHUNK 256
345 static char sync_dump_buf[64 * 1024];
348 struct seq_file s = {
349 .buf = sync_dump_buf,
350 .size = sizeof(sync_dump_buf) - 1,
354 sync_debugfs_show(&s, NULL);
356 for (i = 0; i < s.count; i += DUMP_CHUNK) {
357 if ((s.count - i) > DUMP_CHUNK) {
358 char c = s.buf[i + DUMP_CHUNK];
360 s.buf[i + DUMP_CHUNK] = 0;
361 pr_cont("%s", s.buf + i);
362 s.buf[i + DUMP_CHUNK] = c;
365 pr_cont("%s", s.buf + i);