blob: 06aab07b6bb71bdabd8fa794c39a7b11cb8cb0a0 [file] [log] [blame]
Anton Vorontsov060287b2012-07-09 17:10:41 -07001/*
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
14#include <linux/kernel.h>
15#include <linux/compiler.h>
16#include <linux/irqflags.h>
17#include <linux/percpu.h>
18#include <linux/smp.h>
19#include <linux/atomic.h>
Anton Vorontsov65f8c952012-07-17 14:26:15 -070020#include <linux/types.h>
21#include <linux/mutex.h>
22#include <linux/ftrace.h>
23#include <linux/fs.h>
24#include <linux/debugfs.h>
25#include <linux/err.h>
26#include <linux/cache.h>
Anton Vorontsov060287b2012-07-09 17:10:41 -070027#include <asm/barrier.h>
28#include "internal.h"
29
Joel Fernandesfbccdeb2016-10-20 00:34:05 -070030/* This doesn't need to be atomic: speed is chosen over correctness here. */
31static u64 pstore_ftrace_stamp;
32
Anton Vorontsov65f8c952012-07-17 14:26:15 -070033static void notrace pstore_ftrace_call(unsigned long ip,
Anton Vorontsovebacfd12012-11-14 18:48:15 -080034 unsigned long parent_ip,
35 struct ftrace_ops *op,
36 struct pt_regs *regs)
Anton Vorontsov060287b2012-07-09 17:10:41 -070037{
Anton Vorontsov65f8c952012-07-17 14:26:15 -070038 unsigned long flags;
Anton Vorontsov060287b2012-07-09 17:10:41 -070039 struct pstore_ftrace_record rec = {};
Kees Cookb10b4712017-03-05 00:27:54 -080040 struct pstore_record record = {
41 .type = PSTORE_TYPE_FTRACE,
42 .buf = (char *)&rec,
43 .size = sizeof(rec),
44 .psi = psinfo,
45 };
Anton Vorontsov060287b2012-07-09 17:10:41 -070046
47 if (unlikely(oops_in_progress))
48 return;
49
Anton Vorontsov65f8c952012-07-17 14:26:15 -070050 local_irq_save(flags);
51
Anton Vorontsov060287b2012-07-09 17:10:41 -070052 rec.ip = ip;
53 rec.parent_ip = parent_ip;
Joel Fernandesfbccdeb2016-10-20 00:34:05 -070054 pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
Anton Vorontsov060287b2012-07-09 17:10:41 -070055 pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
Kees Cook4c9ec212017-03-05 22:41:10 -080056 psinfo->write(&record);
Anton Vorontsov65f8c952012-07-17 14:26:15 -070057
58 local_irq_restore(flags);
59}
60
61static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
62 .func = pstore_ftrace_call,
63};
64
65static DEFINE_MUTEX(pstore_ftrace_lock);
66static bool pstore_ftrace_enabled;
67
68static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
69 size_t count, loff_t *ppos)
70{
71 u8 on;
72 ssize_t ret;
73
74 ret = kstrtou8_from_user(buf, count, 2, &on);
75 if (ret)
76 return ret;
77
78 mutex_lock(&pstore_ftrace_lock);
79
80 if (!on ^ pstore_ftrace_enabled)
81 goto out;
82
Joel Fernandes7a0032f2016-11-15 12:31:21 -080083 if (on) {
84 ftrace_ops_set_global_filter(&pstore_ftrace_ops);
Anton Vorontsov65f8c952012-07-17 14:26:15 -070085 ret = register_ftrace_function(&pstore_ftrace_ops);
Joel Fernandes7a0032f2016-11-15 12:31:21 -080086 } else {
Anton Vorontsov65f8c952012-07-17 14:26:15 -070087 ret = unregister_ftrace_function(&pstore_ftrace_ops);
Joel Fernandes7a0032f2016-11-15 12:31:21 -080088 }
89
Anton Vorontsov65f8c952012-07-17 14:26:15 -070090 if (ret) {
91 pr_err("%s: unable to %sregister ftrace ops: %zd\n",
92 __func__, on ? "" : "un", ret);
93 goto err;
94 }
95
96 pstore_ftrace_enabled = on;
97out:
98 ret = count;
99err:
100 mutex_unlock(&pstore_ftrace_lock);
101
102 return ret;
103}
104
105static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
106 size_t count, loff_t *ppos)
107{
108 char val[] = { '0' + pstore_ftrace_enabled, '\n' };
109
110 return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
111}
112
113static const struct file_operations pstore_knob_fops = {
114 .open = simple_open,
115 .read = pstore_ftrace_knob_read,
116 .write = pstore_ftrace_knob_write,
117};
118
Geliang Tangee1d2672015-10-20 00:39:03 -0700119static struct dentry *pstore_ftrace_dir;
120
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700121void pstore_register_ftrace(void)
122{
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700123 struct dentry *file;
124
Kees Cook4c9ec212017-03-05 22:41:10 -0800125 if (!psinfo->write)
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700126 return;
127
Geliang Tangee1d2672015-10-20 00:39:03 -0700128 pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
129 if (!pstore_ftrace_dir) {
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700130 pr_err("%s: unable to create pstore directory\n", __func__);
131 return;
132 }
133
Geliang Tangee1d2672015-10-20 00:39:03 -0700134 file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
135 NULL, &pstore_knob_fops);
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700136 if (!file) {
137 pr_err("%s: unable to create record_ftrace file\n", __func__);
138 goto err_file;
139 }
140
141 return;
142err_file:
Geliang Tangee1d2672015-10-20 00:39:03 -0700143 debugfs_remove(pstore_ftrace_dir);
144}
145
146void pstore_unregister_ftrace(void)
147{
148 mutex_lock(&pstore_ftrace_lock);
149 if (pstore_ftrace_enabled) {
150 unregister_ftrace_function(&pstore_ftrace_ops);
151 pstore_ftrace_enabled = 0;
152 }
153 mutex_unlock(&pstore_ftrace_lock);
154
155 debugfs_remove_recursive(pstore_ftrace_dir);
Anton Vorontsov060287b2012-07-09 17:10:41 -0700156}