2 * Copyright 2012 Google, Inc.
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.
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.
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>
24 #include <linux/debugfs.h>
25 #include <linux/err.h>
26 #include <linux/cache.h>
27 #include <asm/barrier.h>
30 /* This doesn't need to be atomic: speed is chosen over correctness here. */
31 static u64 pstore_ftrace_stamp
;
33 static void notrace
pstore_ftrace_call(unsigned long ip
,
34 unsigned long parent_ip
,
35 struct ftrace_ops
*op
,
39 struct pstore_ftrace_record rec
= {};
40 struct pstore_record record
= {
41 .type
= PSTORE_TYPE_FTRACE
,
47 if (unlikely(oops_in_progress
))
50 local_irq_save(flags
);
53 rec
.parent_ip
= parent_ip
;
54 pstore_ftrace_write_timestamp(&rec
, pstore_ftrace_stamp
++);
55 pstore_ftrace_encode_cpu(&rec
, raw_smp_processor_id());
56 psinfo
->write(&record
);
58 local_irq_restore(flags
);
61 static struct ftrace_ops pstore_ftrace_ops __read_mostly
= {
62 .func
= pstore_ftrace_call
,
65 static DEFINE_MUTEX(pstore_ftrace_lock
);
66 static bool pstore_ftrace_enabled
;
68 static ssize_t
pstore_ftrace_knob_write(struct file
*f
, const char __user
*buf
,
69 size_t count
, loff_t
*ppos
)
74 ret
= kstrtou8_from_user(buf
, count
, 2, &on
);
78 mutex_lock(&pstore_ftrace_lock
);
80 if (!on
^ pstore_ftrace_enabled
)
84 ftrace_ops_set_global_filter(&pstore_ftrace_ops
);
85 ret
= register_ftrace_function(&pstore_ftrace_ops
);
87 ret
= unregister_ftrace_function(&pstore_ftrace_ops
);
91 pr_err("%s: unable to %sregister ftrace ops: %zd\n",
92 __func__
, on
? "" : "un", ret
);
96 pstore_ftrace_enabled
= on
;
100 mutex_unlock(&pstore_ftrace_lock
);
105 static ssize_t
pstore_ftrace_knob_read(struct file
*f
, char __user
*buf
,
106 size_t count
, loff_t
*ppos
)
108 char val
[] = { '0' + pstore_ftrace_enabled
, '\n' };
110 return simple_read_from_buffer(buf
, count
, ppos
, val
, sizeof(val
));
113 static const struct file_operations pstore_knob_fops
= {
115 .read
= pstore_ftrace_knob_read
,
116 .write
= pstore_ftrace_knob_write
,
119 static struct dentry
*pstore_ftrace_dir
;
121 void pstore_register_ftrace(void)
128 pstore_ftrace_dir
= debugfs_create_dir("pstore", NULL
);
129 if (!pstore_ftrace_dir
) {
130 pr_err("%s: unable to create pstore directory\n", __func__
);
134 file
= debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir
,
135 NULL
, &pstore_knob_fops
);
137 pr_err("%s: unable to create record_ftrace file\n", __func__
);
143 debugfs_remove(pstore_ftrace_dir
);
146 void pstore_unregister_ftrace(void)
148 mutex_lock(&pstore_ftrace_lock
);
149 if (pstore_ftrace_enabled
) {
150 unregister_ftrace_function(&pstore_ftrace_ops
);
151 pstore_ftrace_enabled
= 0;
153 mutex_unlock(&pstore_ftrace_lock
);
155 debugfs_remove_recursive(pstore_ftrace_dir
);