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