ftrace.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if (unlikely(oops_in_progress))
  38. return;
  39. local_irq_save(flags);
  40. rec.ip = ip;
  41. rec.parent_ip = parent_ip;
  42. pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
  43. pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
  44. psinfo->write_buf(PSTORE_TYPE_FTRACE, 0, NULL, 0, (void *)&rec,
  45. 0, sizeof(rec), psinfo);
  46. local_irq_restore(flags);
  47. }
  48. static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
  49. .func = pstore_ftrace_call,
  50. };
  51. static DEFINE_MUTEX(pstore_ftrace_lock);
  52. static bool pstore_ftrace_enabled;
  53. static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
  54. size_t count, loff_t *ppos)
  55. {
  56. u8 on;
  57. ssize_t ret;
  58. ret = kstrtou8_from_user(buf, count, 2, &on);
  59. if (ret)
  60. return ret;
  61. mutex_lock(&pstore_ftrace_lock);
  62. if (!on ^ pstore_ftrace_enabled)
  63. goto out;
  64. if (on) {
  65. ftrace_ops_set_global_filter(&pstore_ftrace_ops);
  66. ret = register_ftrace_function(&pstore_ftrace_ops);
  67. } else {
  68. ret = unregister_ftrace_function(&pstore_ftrace_ops);
  69. }
  70. if (ret) {
  71. pr_err("%s: unable to %sregister ftrace ops: %zd\n",
  72. __func__, on ? "" : "un", ret);
  73. goto err;
  74. }
  75. pstore_ftrace_enabled = on;
  76. out:
  77. ret = count;
  78. err:
  79. mutex_unlock(&pstore_ftrace_lock);
  80. return ret;
  81. }
  82. static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
  83. size_t count, loff_t *ppos)
  84. {
  85. char val[] = { '0' + pstore_ftrace_enabled, '\n' };
  86. return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
  87. }
  88. static const struct file_operations pstore_knob_fops = {
  89. .open = simple_open,
  90. .read = pstore_ftrace_knob_read,
  91. .write = pstore_ftrace_knob_write,
  92. };
  93. static struct dentry *pstore_ftrace_dir;
  94. void pstore_register_ftrace(void)
  95. {
  96. struct dentry *file;
  97. if (!psinfo->write_buf)
  98. return;
  99. pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
  100. if (!pstore_ftrace_dir) {
  101. pr_err("%s: unable to create pstore directory\n", __func__);
  102. return;
  103. }
  104. file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
  105. NULL, &pstore_knob_fops);
  106. if (!file) {
  107. pr_err("%s: unable to create record_ftrace file\n", __func__);
  108. goto err_file;
  109. }
  110. return;
  111. err_file:
  112. debugfs_remove(pstore_ftrace_dir);
  113. }
  114. void pstore_unregister_ftrace(void)
  115. {
  116. mutex_lock(&pstore_ftrace_lock);
  117. if (pstore_ftrace_enabled) {
  118. unregister_ftrace_function(&pstore_ftrace_ops);
  119. pstore_ftrace_enabled = 0;
  120. }
  121. mutex_unlock(&pstore_ftrace_lock);
  122. debugfs_remove_recursive(pstore_ftrace_dir);
  123. }