sync_debug.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Sync File validation framework and debug information
  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. #include <linux/debugfs.h>
  17. #include "sync_debug.h"
  18. static struct dentry *dbgfs;
  19. static LIST_HEAD(sync_timeline_list_head);
  20. static DEFINE_SPINLOCK(sync_timeline_list_lock);
  21. static LIST_HEAD(sync_file_list_head);
  22. static DEFINE_SPINLOCK(sync_file_list_lock);
  23. void sync_timeline_debug_add(struct sync_timeline *obj)
  24. {
  25. unsigned long flags;
  26. spin_lock_irqsave(&sync_timeline_list_lock, flags);
  27. list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
  28. spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
  29. }
  30. void sync_timeline_debug_remove(struct sync_timeline *obj)
  31. {
  32. unsigned long flags;
  33. spin_lock_irqsave(&sync_timeline_list_lock, flags);
  34. list_del(&obj->sync_timeline_list);
  35. spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
  36. }
  37. void sync_file_debug_add(struct sync_file *sync_file)
  38. {
  39. unsigned long flags;
  40. spin_lock_irqsave(&sync_file_list_lock, flags);
  41. list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
  42. spin_unlock_irqrestore(&sync_file_list_lock, flags);
  43. }
  44. void sync_file_debug_remove(struct sync_file *sync_file)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&sync_file_list_lock, flags);
  48. list_del(&sync_file->sync_file_list);
  49. spin_unlock_irqrestore(&sync_file_list_lock, flags);
  50. }
  51. static const char *sync_status_str(int status)
  52. {
  53. if (status < 0)
  54. return "error";
  55. if (status > 0)
  56. return "signaled";
  57. return "active";
  58. }
  59. static void sync_print_fence(struct seq_file *s,
  60. struct dma_fence *fence, bool show)
  61. {
  62. struct sync_timeline *parent = dma_fence_parent(fence);
  63. int status;
  64. status = dma_fence_get_status_locked(fence);
  65. seq_printf(s, " %s%sfence %s",
  66. show ? parent->name : "",
  67. show ? "_" : "",
  68. sync_status_str(status));
  69. if (status) {
  70. struct timespec64 ts64 =
  71. ktime_to_timespec64(fence->timestamp);
  72. seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
  73. }
  74. if (fence->ops->timeline_value_str &&
  75. fence->ops->fence_value_str) {
  76. char value[64];
  77. bool success;
  78. fence->ops->fence_value_str(fence, value, sizeof(value));
  79. success = strlen(value);
  80. if (success) {
  81. seq_printf(s, ": %s", value);
  82. fence->ops->timeline_value_str(fence, value,
  83. sizeof(value));
  84. if (strlen(value))
  85. seq_printf(s, " / %s", value);
  86. }
  87. }
  88. seq_putc(s, '\n');
  89. }
  90. static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
  91. {
  92. struct list_head *pos;
  93. unsigned long flags;
  94. seq_printf(s, "%s: %d\n", obj->name, obj->value);
  95. spin_lock_irqsave(&obj->child_list_lock, flags);
  96. list_for_each(pos, &obj->child_list_head) {
  97. struct sync_pt *pt =
  98. container_of(pos, struct sync_pt, child_list);
  99. sync_print_fence(s, &pt->base, false);
  100. }
  101. spin_unlock_irqrestore(&obj->child_list_lock, flags);
  102. }
  103. static void sync_print_sync_file(struct seq_file *s,
  104. struct sync_file *sync_file)
  105. {
  106. char buf[128];
  107. int i;
  108. seq_printf(s, "[%p] %s: %s\n", sync_file,
  109. sync_file_get_name(sync_file, buf, sizeof(buf)),
  110. sync_status_str(dma_fence_get_status(sync_file->fence)));
  111. if (dma_fence_is_array(sync_file->fence)) {
  112. struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
  113. for (i = 0; i < array->num_fences; ++i)
  114. sync_print_fence(s, array->fences[i], true);
  115. } else {
  116. sync_print_fence(s, sync_file->fence, true);
  117. }
  118. }
  119. static int sync_debugfs_show(struct seq_file *s, void *unused)
  120. {
  121. unsigned long flags;
  122. struct list_head *pos;
  123. seq_puts(s, "objs:\n--------------\n");
  124. spin_lock_irqsave(&sync_timeline_list_lock, flags);
  125. list_for_each(pos, &sync_timeline_list_head) {
  126. struct sync_timeline *obj =
  127. container_of(pos, struct sync_timeline,
  128. sync_timeline_list);
  129. sync_print_obj(s, obj);
  130. seq_putc(s, '\n');
  131. }
  132. spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
  133. seq_puts(s, "fences:\n--------------\n");
  134. spin_lock_irqsave(&sync_file_list_lock, flags);
  135. list_for_each(pos, &sync_file_list_head) {
  136. struct sync_file *sync_file =
  137. container_of(pos, struct sync_file, sync_file_list);
  138. sync_print_sync_file(s, sync_file);
  139. seq_putc(s, '\n');
  140. }
  141. spin_unlock_irqrestore(&sync_file_list_lock, flags);
  142. return 0;
  143. }
  144. static int sync_info_debugfs_open(struct inode *inode, struct file *file)
  145. {
  146. return single_open(file, sync_debugfs_show, inode->i_private);
  147. }
  148. static const struct file_operations sync_info_debugfs_fops = {
  149. .open = sync_info_debugfs_open,
  150. .read = seq_read,
  151. .llseek = seq_lseek,
  152. .release = single_release,
  153. };
  154. static __init int sync_debugfs_init(void)
  155. {
  156. dbgfs = debugfs_create_dir("sync", NULL);
  157. /*
  158. * The debugfs files won't ever get removed and thus, there is
  159. * no need to protect it against removal races. The use of
  160. * debugfs_create_file_unsafe() is actually safe here.
  161. */
  162. debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
  163. &sync_info_debugfs_fops);
  164. debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
  165. &sw_sync_debugfs_fops);
  166. return 0;
  167. }
  168. late_initcall(sync_debugfs_init);
  169. #define DUMP_CHUNK 256
  170. static char sync_dump_buf[64 * 1024];
  171. void sync_dump(void)
  172. {
  173. struct seq_file s = {
  174. .buf = sync_dump_buf,
  175. .size = sizeof(sync_dump_buf) - 1,
  176. };
  177. int i;
  178. sync_debugfs_show(&s, NULL);
  179. for (i = 0; i < s.count; i += DUMP_CHUNK) {
  180. if ((s.count - i) > DUMP_CHUNK) {
  181. char c = s.buf[i + DUMP_CHUNK];
  182. s.buf[i + DUMP_CHUNK] = 0;
  183. pr_cont("%s", s.buf + i);
  184. s.buf[i + DUMP_CHUNK] = c;
  185. } else {
  186. s.buf[s.count] = 0;
  187. pr_cont("%s", s.buf + i);
  188. }
  189. }
  190. }