ftrace.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2012 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/compiler.h>
  15. #include <linux/irqflags.h>
  16. #include <linux/percpu.h>
  17. #include <linux/smp.h>
  18. #include <linux/atomic.h>
  19. #include <linux/types.h>
  20. #include <linux/mutex.h>
  21. #include <linux/ftrace.h>
  22. #include <linux/fs.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/err.h>
  25. #include <linux/cache.h>
  26. #include <asm/barrier.h>
  27. #include "internal.h"
  28. /* This doesn't need to be atomic: speed is chosen over correctness here. */
  29. static u64 pstore_ftrace_stamp;
  30. static void notrace pstore_ftrace_call(unsigned long ip,
  31. unsigned long parent_ip,
  32. struct ftrace_ops *op,
  33. struct pt_regs *regs)
  34. {
  35. unsigned long flags;
  36. struct pstore_ftrace_record rec = {};
  37. struct pstore_record record = {
  38. .type = PSTORE_TYPE_FTRACE,
  39. .buf = (char *)&rec,
  40. .size = sizeof(rec),
  41. .psi = psinfo,
  42. };
  43. if (unlikely(oops_in_progress))
  44. return;
  45. local_irq_save(flags);
  46. rec.ip = ip;
  47. rec.parent_ip = parent_ip;
  48. pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
  49. pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
  50. psinfo->write(&record);
  51. local_irq_restore(flags);
  52. }
  53. static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
  54. .func = pstore_ftrace_call,
  55. };
  56. static DEFINE_MUTEX(pstore_ftrace_lock);
  57. static bool pstore_ftrace_enabled;
  58. static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
  59. size_t count, loff_t *ppos)
  60. {
  61. u8 on;
  62. ssize_t ret;
  63. ret = kstrtou8_from_user(buf, count, 2, &on);
  64. if (ret)
  65. return ret;
  66. mutex_lock(&pstore_ftrace_lock);
  67. if (!on ^ pstore_ftrace_enabled)
  68. goto out;
  69. if (on) {
  70. ftrace_ops_set_global_filter(&pstore_ftrace_ops);
  71. ret = register_ftrace_function(&pstore_ftrace_ops);
  72. } else {
  73. ret = unregister_ftrace_function(&pstore_ftrace_ops);
  74. }
  75. if (ret) {
  76. pr_err("%s: unable to %sregister ftrace ops: %zd\n",
  77. __func__, on ? "" : "un", ret);
  78. goto err;
  79. }
  80. pstore_ftrace_enabled = on;
  81. out:
  82. ret = count;
  83. err:
  84. mutex_unlock(&pstore_ftrace_lock);
  85. return ret;
  86. }
  87. static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
  88. size_t count, loff_t *ppos)
  89. {
  90. char val[] = { '0' + pstore_ftrace_enabled, '\n' };
  91. return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
  92. }
  93. static const struct file_operations pstore_knob_fops = {
  94. .open = simple_open,
  95. .read = pstore_ftrace_knob_read,
  96. .write = pstore_ftrace_knob_write,
  97. };
  98. static struct dentry *pstore_ftrace_dir;
  99. void pstore_register_ftrace(void)
  100. {
  101. struct dentry *file;
  102. if (!psinfo->write)
  103. return;
  104. pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
  105. if (!pstore_ftrace_dir) {
  106. pr_err("%s: unable to create pstore directory\n", __func__);
  107. return;
  108. }
  109. file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
  110. NULL, &pstore_knob_fops);
  111. if (!file) {
  112. pr_err("%s: unable to create record_ftrace file\n", __func__);
  113. goto err_file;
  114. }
  115. return;
  116. err_file:
  117. debugfs_remove(pstore_ftrace_dir);
  118. }
  119. void pstore_unregister_ftrace(void)
  120. {
  121. mutex_lock(&pstore_ftrace_lock);
  122. if (pstore_ftrace_enabled) {
  123. unregister_ftrace_function(&pstore_ftrace_ops);
  124. pstore_ftrace_enabled = 0;
  125. }
  126. mutex_unlock(&pstore_ftrace_lock);
  127. debugfs_remove_recursive(pstore_ftrace_dir);
  128. }