ftrace.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 pt_regs *regs)
  27. {
  28. unsigned long flags;
  29. struct pstore_ftrace_record rec = {};
  30. struct pstore_record record = {
  31. .type = PSTORE_TYPE_FTRACE,
  32. .buf = (char *)&rec,
  33. .size = sizeof(rec),
  34. .psi = psinfo,
  35. };
  36. if (unlikely(oops_in_progress))
  37. return;
  38. local_irq_save(flags);
  39. rec.ip = ip;
  40. rec.parent_ip = parent_ip;
  41. pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
  42. pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
  43. psinfo->write(&record);
  44. local_irq_restore(flags);
  45. }
  46. static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
  47. .func = pstore_ftrace_call,
  48. };
  49. static DEFINE_MUTEX(pstore_ftrace_lock);
  50. static bool pstore_ftrace_enabled;
  51. static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
  52. size_t count, loff_t *ppos)
  53. {
  54. u8 on;
  55. ssize_t ret;
  56. ret = kstrtou8_from_user(buf, count, 2, &on);
  57. if (ret)
  58. return ret;
  59. mutex_lock(&pstore_ftrace_lock);
  60. if (!on ^ pstore_ftrace_enabled)
  61. goto out;
  62. if (on) {
  63. ftrace_ops_set_global_filter(&pstore_ftrace_ops);
  64. ret = register_ftrace_function(&pstore_ftrace_ops);
  65. } else {
  66. ret = unregister_ftrace_function(&pstore_ftrace_ops);
  67. }
  68. if (ret) {
  69. pr_err("%s: unable to %sregister ftrace ops: %zd\n",
  70. __func__, on ? "" : "un", ret);
  71. goto err;
  72. }
  73. pstore_ftrace_enabled = on;
  74. out:
  75. ret = count;
  76. err:
  77. mutex_unlock(&pstore_ftrace_lock);
  78. return ret;
  79. }
  80. static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
  81. size_t count, loff_t *ppos)
  82. {
  83. char val[] = { '0' + pstore_ftrace_enabled, '\n' };
  84. return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
  85. }
  86. static const struct file_operations pstore_knob_fops = {
  87. .open = simple_open,
  88. .read = pstore_ftrace_knob_read,
  89. .write = pstore_ftrace_knob_write,
  90. };
  91. static struct dentry *pstore_ftrace_dir;
  92. void pstore_register_ftrace(void)
  93. {
  94. if (!psinfo->write)
  95. return;
  96. pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
  97. debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir, NULL,
  98. &pstore_knob_fops);
  99. }
  100. void pstore_unregister_ftrace(void)
  101. {
  102. mutex_lock(&pstore_ftrace_lock);
  103. if (pstore_ftrace_enabled) {
  104. unregister_ftrace_function(&pstore_ftrace_ops);
  105. pstore_ftrace_enabled = false;
  106. }
  107. mutex_unlock(&pstore_ftrace_lock);
  108. debugfs_remove_recursive(pstore_ftrace_dir);
  109. }
  110. ssize_t pstore_ftrace_combine_log(char **dest_log, size_t *dest_log_size,
  111. const char *src_log, size_t src_log_size)
  112. {
  113. size_t dest_size, src_size, total, dest_off, src_off;
  114. size_t dest_idx = 0, src_idx = 0, merged_idx = 0;
  115. void *merged_buf;
  116. struct pstore_ftrace_record *drec, *srec, *mrec;
  117. size_t record_size = sizeof(struct pstore_ftrace_record);
  118. dest_off = *dest_log_size % record_size;
  119. dest_size = *dest_log_size - dest_off;
  120. src_off = src_log_size % record_size;
  121. src_size = src_log_size - src_off;
  122. total = dest_size + src_size;
  123. merged_buf = kmalloc(total, GFP_KERNEL);
  124. if (!merged_buf)
  125. return -ENOMEM;
  126. drec = (struct pstore_ftrace_record *)(*dest_log + dest_off);
  127. srec = (struct pstore_ftrace_record *)(src_log + src_off);
  128. mrec = (struct pstore_ftrace_record *)(merged_buf);
  129. while (dest_size > 0 && src_size > 0) {
  130. if (pstore_ftrace_read_timestamp(&drec[dest_idx]) <
  131. pstore_ftrace_read_timestamp(&srec[src_idx])) {
  132. mrec[merged_idx++] = drec[dest_idx++];
  133. dest_size -= record_size;
  134. } else {
  135. mrec[merged_idx++] = srec[src_idx++];
  136. src_size -= record_size;
  137. }
  138. }
  139. while (dest_size > 0) {
  140. mrec[merged_idx++] = drec[dest_idx++];
  141. dest_size -= record_size;
  142. }
  143. while (src_size > 0) {
  144. mrec[merged_idx++] = srec[src_idx++];
  145. src_size -= record_size;
  146. }
  147. kfree(*dest_log);
  148. *dest_log = merged_buf;
  149. *dest_log_size = total;
  150. return 0;
  151. }
  152. EXPORT_SYMBOL_GPL(pstore_ftrace_combine_log);