ftrace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2012 Google, Inc.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/compiler.h>
  7. #include <linux/irqflags.h>
  8. #include <linux/percpu.h>
  9. #include <linux/smp.h>
  10. #include <linux/atomic.h>
  11. #include <linux/types.h>
  12. #include <linux/mutex.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/fs.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/err.h>
  17. #include <linux/cache.h>
  18. #include <linux/slab.h>
  19. #include <asm/barrier.h>
  20. #include "internal.h"
  21. /* This doesn't need to be atomic: speed is chosen over correctness here. */
  22. static u64 pstore_ftrace_stamp;
  23. static void notrace pstore_ftrace_call(unsigned long ip,
  24. unsigned long parent_ip,
  25. struct ftrace_ops *op,
  26. struct ftrace_regs *fregs)
  27. {
  28. int bit;
  29. unsigned long flags;
  30. struct pstore_ftrace_record rec = {};
  31. struct pstore_record record = {
  32. .type = PSTORE_TYPE_FTRACE,
  33. .buf = (char *)&rec,
  34. .size = sizeof(rec),
  35. .psi = psinfo,
  36. };
  37. if (unlikely(oops_in_progress))
  38. return;
  39. bit = ftrace_test_recursion_trylock(ip, parent_ip);
  40. if (bit < 0)
  41. return;
  42. local_irq_save(flags);
  43. rec.ip = ip;
  44. rec.parent_ip = parent_ip;
  45. pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
  46. pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
  47. psinfo->write(&record);
  48. local_irq_restore(flags);
  49. ftrace_test_recursion_unlock(bit);
  50. }
  51. static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
  52. .func = pstore_ftrace_call,
  53. };
  54. static DEFINE_MUTEX(pstore_ftrace_lock);
  55. static bool pstore_ftrace_enabled;
  56. static int pstore_set_ftrace_enabled(bool on)
  57. {
  58. ssize_t ret;
  59. if (on == pstore_ftrace_enabled)
  60. return 0;
  61. if (on) {
  62. ftrace_ops_set_global_filter(&pstore_ftrace_ops);
  63. ret = register_ftrace_function(&pstore_ftrace_ops);
  64. } else {
  65. ret = unregister_ftrace_function(&pstore_ftrace_ops);
  66. }
  67. if (ret) {
  68. pr_err("%s: unable to %sregister ftrace ops: %zd\n",
  69. __func__, on ? "" : "un", ret);
  70. } else {
  71. pstore_ftrace_enabled = on;
  72. }
  73. return ret;
  74. }
  75. static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
  76. size_t count, loff_t *ppos)
  77. {
  78. u8 on;
  79. ssize_t ret;
  80. ret = kstrtou8_from_user(buf, count, 2, &on);
  81. if (ret)
  82. return ret;
  83. mutex_lock(&pstore_ftrace_lock);
  84. ret = pstore_set_ftrace_enabled(on);
  85. mutex_unlock(&pstore_ftrace_lock);
  86. if (ret == 0)
  87. ret = count;
  88. return ret;
  89. }
  90. static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
  91. size_t count, loff_t *ppos)
  92. {
  93. char val[] = { '0' + pstore_ftrace_enabled, '\n' };
  94. return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
  95. }
  96. static const struct file_operations pstore_knob_fops = {
  97. .open = simple_open,
  98. .read = pstore_ftrace_knob_read,
  99. .write = pstore_ftrace_knob_write,
  100. };
  101. static struct dentry *pstore_ftrace_dir;
  102. static bool record_ftrace;
  103. module_param(record_ftrace, bool, 0400);
  104. MODULE_PARM_DESC(record_ftrace,
  105. "enable ftrace recording immediately (default: off)");
  106. void pstore_register_ftrace(void)
  107. {
  108. if (!psinfo->write)
  109. return;
  110. pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
  111. pstore_set_ftrace_enabled(record_ftrace);
  112. debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir, NULL,
  113. &pstore_knob_fops);
  114. }
  115. void pstore_unregister_ftrace(void)
  116. {
  117. mutex_lock(&pstore_ftrace_lock);
  118. if (pstore_ftrace_enabled) {
  119. unregister_ftrace_function(&pstore_ftrace_ops);
  120. pstore_ftrace_enabled = false;
  121. }
  122. mutex_unlock(&pstore_ftrace_lock);
  123. debugfs_remove_recursive(pstore_ftrace_dir);
  124. }
  125. ssize_t pstore_ftrace_combine_log(char **dest_log, size_t *dest_log_size,
  126. const char *src_log, size_t src_log_size)
  127. {
  128. size_t dest_size, src_size, total, dest_off, src_off;
  129. size_t dest_idx = 0, src_idx = 0, merged_idx = 0;
  130. void *merged_buf;
  131. struct pstore_ftrace_record *drec, *srec, *mrec;
  132. size_t record_size = sizeof(struct pstore_ftrace_record);
  133. dest_off = *dest_log_size % record_size;
  134. dest_size = *dest_log_size - dest_off;
  135. src_off = src_log_size % record_size;
  136. src_size = src_log_size - src_off;
  137. total = dest_size + src_size;
  138. merged_buf = kmalloc(total, GFP_KERNEL);
  139. if (!merged_buf)
  140. return -ENOMEM;
  141. drec = (struct pstore_ftrace_record *)(*dest_log + dest_off);
  142. srec = (struct pstore_ftrace_record *)(src_log + src_off);
  143. mrec = (struct pstore_ftrace_record *)(merged_buf);
  144. while (dest_size > 0 && src_size > 0) {
  145. if (pstore_ftrace_read_timestamp(&drec[dest_idx]) <
  146. pstore_ftrace_read_timestamp(&srec[src_idx])) {
  147. mrec[merged_idx++] = drec[dest_idx++];
  148. dest_size -= record_size;
  149. } else {
  150. mrec[merged_idx++] = srec[src_idx++];
  151. src_size -= record_size;
  152. }
  153. }
  154. while (dest_size > 0) {
  155. mrec[merged_idx++] = drec[dest_idx++];
  156. dest_size -= record_size;
  157. }
  158. while (src_size > 0) {
  159. mrec[merged_idx++] = srec[src_idx++];
  160. src_size -= record_size;
  161. }
  162. kfree(*dest_log);
  163. *dest_log = merged_buf;
  164. *dest_log_size = total;
  165. return 0;
  166. }
  167. EXPORT_SYMBOL_GPL(pstore_ftrace_combine_log);