Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2012 Google, Inc.
4 */
5
6#include <linux/kernel.h>
7#include <linux/compiler.h>
8#include <linux/irqflags.h>
9#include <linux/percpu.h>
10#include <linux/smp.h>
11#include <linux/atomic.h>
12#include <linux/types.h>
13#include <linux/mutex.h>
14#include <linux/ftrace.h>
15#include <linux/fs.h>
16#include <linux/debugfs.h>
17#include <linux/err.h>
18#include <linux/cache.h>
19#include <asm/barrier.h>
20#include "internal.h"
21
22/* This doesn't need to be atomic: speed is chosen over correctness here. */
23static u64 pstore_ftrace_stamp;
24
25static void notrace pstore_ftrace_call(unsigned long ip,
26 unsigned long parent_ip,
27 struct ftrace_ops *op,
28 struct pt_regs *regs)
29{
30 unsigned long flags;
31 struct pstore_ftrace_record rec = {};
32 struct pstore_record record = {
33 .type = PSTORE_TYPE_FTRACE,
34 .buf = (char *)&rec,
35 .size = sizeof(rec),
36 .psi = psinfo,
37 };
38
39 if (unlikely(oops_in_progress))
40 return;
41
42 local_irq_save(flags);
43
44 rec.ip = ip;
45 rec.parent_ip = parent_ip;
46 pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
47 pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
48 psinfo->write(&record);
49
50 local_irq_restore(flags);
51}
52
53static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
54 .func = pstore_ftrace_call,
55};
56
57static DEFINE_MUTEX(pstore_ftrace_lock);
58static bool pstore_ftrace_enabled;
59
60static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
61 size_t count, loff_t *ppos)
62{
63 u8 on;
64 ssize_t ret;
65
66 ret = kstrtou8_from_user(buf, count, 2, &on);
67 if (ret)
68 return ret;
69
70 mutex_lock(&pstore_ftrace_lock);
71
72 if (!on ^ pstore_ftrace_enabled)
73 goto out;
74
75 if (on) {
76 ftrace_ops_set_global_filter(&pstore_ftrace_ops);
77 ret = register_ftrace_function(&pstore_ftrace_ops);
78 } else {
79 ret = unregister_ftrace_function(&pstore_ftrace_ops);
80 }
81
82 if (ret) {
83 pr_err("%s: unable to %sregister ftrace ops: %zd\n",
84 __func__, on ? "" : "un", ret);
85 goto err;
86 }
87
88 pstore_ftrace_enabled = on;
89out:
90 ret = count;
91err:
92 mutex_unlock(&pstore_ftrace_lock);
93
94 return ret;
95}
96
97static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
98 size_t count, loff_t *ppos)
99{
100 char val[] = { '0' + pstore_ftrace_enabled, '\n' };
101
102 return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
103}
104
105static const struct file_operations pstore_knob_fops = {
106 .open = simple_open,
107 .read = pstore_ftrace_knob_read,
108 .write = pstore_ftrace_knob_write,
109};
110
111static struct dentry *pstore_ftrace_dir;
112
113void pstore_register_ftrace(void)
114{
115 if (!psinfo->write)
116 return;
117
118 pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
119
120 debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir, NULL,
121 &pstore_knob_fops);
122}
123
124void pstore_unregister_ftrace(void)
125{
126 mutex_lock(&pstore_ftrace_lock);
127 if (pstore_ftrace_enabled) {
128 unregister_ftrace_function(&pstore_ftrace_ops);
129 pstore_ftrace_enabled = false;
130 }
131 mutex_unlock(&pstore_ftrace_lock);
132
133 debugfs_remove_recursive(pstore_ftrace_dir);
134}
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
30static 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
38 if (unlikely(oops_in_progress))
39 return;
40
41 local_irq_save(flags);
42
43 rec.ip = ip;
44 rec.parent_ip = parent_ip;
45 pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
46 psinfo->write_buf(PSTORE_TYPE_FTRACE, 0, NULL, 0, (void *)&rec,
47 0, sizeof(rec), psinfo);
48
49 local_irq_restore(flags);
50}
51
52static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
53 .func = pstore_ftrace_call,
54};
55
56static DEFINE_MUTEX(pstore_ftrace_lock);
57static bool pstore_ftrace_enabled;
58
59static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
60 size_t count, loff_t *ppos)
61{
62 u8 on;
63 ssize_t ret;
64
65 ret = kstrtou8_from_user(buf, count, 2, &on);
66 if (ret)
67 return ret;
68
69 mutex_lock(&pstore_ftrace_lock);
70
71 if (!on ^ pstore_ftrace_enabled)
72 goto out;
73
74 if (on)
75 ret = register_ftrace_function(&pstore_ftrace_ops);
76 else
77 ret = unregister_ftrace_function(&pstore_ftrace_ops);
78 if (ret) {
79 pr_err("%s: unable to %sregister ftrace ops: %zd\n",
80 __func__, on ? "" : "un", ret);
81 goto err;
82 }
83
84 pstore_ftrace_enabled = on;
85out:
86 ret = count;
87err:
88 mutex_unlock(&pstore_ftrace_lock);
89
90 return ret;
91}
92
93static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
94 size_t count, loff_t *ppos)
95{
96 char val[] = { '0' + pstore_ftrace_enabled, '\n' };
97
98 return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
99}
100
101static const struct file_operations pstore_knob_fops = {
102 .open = simple_open,
103 .read = pstore_ftrace_knob_read,
104 .write = pstore_ftrace_knob_write,
105};
106
107static struct dentry *pstore_ftrace_dir;
108
109void pstore_register_ftrace(void)
110{
111 struct dentry *file;
112
113 if (!psinfo->write_buf)
114 return;
115
116 pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
117 if (!pstore_ftrace_dir) {
118 pr_err("%s: unable to create pstore directory\n", __func__);
119 return;
120 }
121
122 file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
123 NULL, &pstore_knob_fops);
124 if (!file) {
125 pr_err("%s: unable to create record_ftrace file\n", __func__);
126 goto err_file;
127 }
128
129 return;
130err_file:
131 debugfs_remove(pstore_ftrace_dir);
132}
133
134void pstore_unregister_ftrace(void)
135{
136 mutex_lock(&pstore_ftrace_lock);
137 if (pstore_ftrace_enabled) {
138 unregister_ftrace_function(&pstore_ftrace_ops);
139 pstore_ftrace_enabled = 0;
140 }
141 mutex_unlock(&pstore_ftrace_lock);
142
143 debugfs_remove_recursive(pstore_ftrace_dir);
144}