sync_debug.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 fence *fence, bool show)
  61. {
  62. struct sync_timeline *parent = fence_parent(fence);
  63. int status;
  64. status = 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_puts(s, "\n");
  89. }
  90. static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
  91. {
  92. struct list_head *pos;
  93. seq_printf(s, "%s: %d\n", obj->name, obj->value);
  94. spin_lock_irq(&obj->lock);
  95. list_for_each(pos, &obj->pt_list) {
  96. struct sync_pt *pt = container_of(pos, struct sync_pt, link);
  97. sync_print_fence(s, &pt->base, false);
  98. }
  99. spin_unlock_irq(&obj->lock);
  100. }
  101. static void sync_print_sync_file(struct seq_file *s,
  102. struct sync_file *sync_file)
  103. {
  104. int i;
  105. seq_printf(s, "[%p] %s: %s\n", sync_file, sync_file->name,
  106. sync_status_str(fence_get_status(sync_file->fence)));
  107. if (fence_is_array(sync_file->fence)) {
  108. struct fence_array *array = to_fence_array(sync_file->fence);
  109. for (i = 0; i < array->num_fences; ++i)
  110. sync_print_fence(s, array->fences[i], true);
  111. } else {
  112. sync_print_fence(s, sync_file->fence, true);
  113. }
  114. }
  115. static int sync_debugfs_show(struct seq_file *s, void *unused)
  116. {
  117. struct list_head *pos;
  118. seq_puts(s, "objs:\n--------------\n");
  119. spin_lock_irq(&sync_timeline_list_lock);
  120. list_for_each(pos, &sync_timeline_list_head) {
  121. struct sync_timeline *obj =
  122. container_of(pos, struct sync_timeline,
  123. sync_timeline_list);
  124. sync_print_obj(s, obj);
  125. seq_puts(s, "\n");
  126. }
  127. spin_unlock_irq(&sync_timeline_list_lock);
  128. seq_puts(s, "fences:\n--------------\n");
  129. spin_lock_irq(&sync_file_list_lock);
  130. list_for_each(pos, &sync_file_list_head) {
  131. struct sync_file *sync_file =
  132. container_of(pos, struct sync_file, sync_file_list);
  133. sync_print_sync_file(s, sync_file);
  134. seq_puts(s, "\n");
  135. }
  136. spin_unlock_irq(&sync_file_list_lock);
  137. return 0;
  138. }
  139. static int sync_info_debugfs_open(struct inode *inode, struct file *file)
  140. {
  141. return single_open(file, sync_debugfs_show, inode->i_private);
  142. }
  143. static const struct file_operations sync_info_debugfs_fops = {
  144. .open = sync_info_debugfs_open,
  145. .read = seq_read,
  146. .llseek = seq_lseek,
  147. .release = single_release,
  148. };
  149. static __init int sync_debugfs_init(void)
  150. {
  151. dbgfs = debugfs_create_dir("sync", NULL);
  152. /*
  153. * The debugfs files won't ever get removed and thus, there is
  154. * no need to protect it against removal races. The use of
  155. * debugfs_create_file_unsafe() is actually safe here.
  156. */
  157. debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
  158. &sync_info_debugfs_fops);
  159. debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
  160. &sw_sync_debugfs_fops);
  161. return 0;
  162. }
  163. late_initcall(sync_debugfs_init);
  164. #define DUMP_CHUNK 256
  165. static char sync_dump_buf[64 * 1024];
  166. void sync_dump(void)
  167. {
  168. struct seq_file s = {
  169. .buf = sync_dump_buf,
  170. .size = sizeof(sync_dump_buf) - 1,
  171. };
  172. int i;
  173. sync_debugfs_show(&s, NULL);
  174. for (i = 0; i < s.count; i += DUMP_CHUNK) {
  175. if ((s.count - i) > DUMP_CHUNK) {
  176. char c = s.buf[i + DUMP_CHUNK];
  177. s.buf[i + DUMP_CHUNK] = 0;
  178. pr_cont("%s", s.buf + i);
  179. s.buf[i + DUMP_CHUNK] = c;
  180. } else {
  181. s.buf[s.count] = 0;
  182. pr_cont("%s", s.buf + i);
  183. }
  184. }
  185. }