blob: 8e0a17ce3180bc5bd172252a86e748d5f8f912fc [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Anton Vorontsov060287b2012-07-09 17:10:41 -07002/*
3 * Copyright 2012 Google, Inc.
Anton Vorontsov060287b2012-07-09 17:10:41 -07004 */
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>
Anton Vorontsov65f8c952012-07-17 14:26:15 -070012#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>
Anton Vorontsov060287b2012-07-09 17:10:41 -070019#include <asm/barrier.h>
20#include "internal.h"
21
Joel Fernandesfbccdeb2016-10-20 00:34:05 -070022/* This doesn't need to be atomic: speed is chosen over correctness here. */
23static u64 pstore_ftrace_stamp;
24
Anton Vorontsov65f8c952012-07-17 14:26:15 -070025static void notrace pstore_ftrace_call(unsigned long ip,
Anton Vorontsovebacfd12012-11-14 18:48:15 -080026 unsigned long parent_ip,
27 struct ftrace_ops *op,
28 struct pt_regs *regs)
Anton Vorontsov060287b2012-07-09 17:10:41 -070029{
Anton Vorontsov65f8c952012-07-17 14:26:15 -070030 unsigned long flags;
Anton Vorontsov060287b2012-07-09 17:10:41 -070031 struct pstore_ftrace_record rec = {};
Kees Cookb10b4712017-03-05 00:27:54 -080032 struct pstore_record record = {
33 .type = PSTORE_TYPE_FTRACE,
34 .buf = (char *)&rec,
35 .size = sizeof(rec),
36 .psi = psinfo,
37 };
Anton Vorontsov060287b2012-07-09 17:10:41 -070038
39 if (unlikely(oops_in_progress))
40 return;
41
Anton Vorontsov65f8c952012-07-17 14:26:15 -070042 local_irq_save(flags);
43
Anton Vorontsov060287b2012-07-09 17:10:41 -070044 rec.ip = ip;
45 rec.parent_ip = parent_ip;
Joel Fernandesfbccdeb2016-10-20 00:34:05 -070046 pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
Anton Vorontsov060287b2012-07-09 17:10:41 -070047 pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
Kees Cook4c9ec212017-03-05 22:41:10 -080048 psinfo->write(&record);
Anton Vorontsov65f8c952012-07-17 14:26:15 -070049
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
Joel Fernandes7a0032f2016-11-15 12:31:21 -080075 if (on) {
76 ftrace_ops_set_global_filter(&pstore_ftrace_ops);
Anton Vorontsov65f8c952012-07-17 14:26:15 -070077 ret = register_ftrace_function(&pstore_ftrace_ops);
Joel Fernandes7a0032f2016-11-15 12:31:21 -080078 } else {
Anton Vorontsov65f8c952012-07-17 14:26:15 -070079 ret = unregister_ftrace_function(&pstore_ftrace_ops);
Joel Fernandes7a0032f2016-11-15 12:31:21 -080080 }
81
Anton Vorontsov65f8c952012-07-17 14:26:15 -070082 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
Geliang Tangee1d2672015-10-20 00:39:03 -0700111static struct dentry *pstore_ftrace_dir;
112
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700113void pstore_register_ftrace(void)
114{
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700115 struct dentry *file;
116
Kees Cook4c9ec212017-03-05 22:41:10 -0800117 if (!psinfo->write)
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700118 return;
119
Geliang Tangee1d2672015-10-20 00:39:03 -0700120 pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
121 if (!pstore_ftrace_dir) {
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700122 pr_err("%s: unable to create pstore directory\n", __func__);
123 return;
124 }
125
Geliang Tangee1d2672015-10-20 00:39:03 -0700126 file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
127 NULL, &pstore_knob_fops);
Anton Vorontsov65f8c952012-07-17 14:26:15 -0700128 if (!file) {
129 pr_err("%s: unable to create record_ftrace file\n", __func__);
130 goto err_file;
131 }
132
133 return;
134err_file:
Geliang Tangee1d2672015-10-20 00:39:03 -0700135 debugfs_remove(pstore_ftrace_dir);
136}
137
138void pstore_unregister_ftrace(void)
139{
140 mutex_lock(&pstore_ftrace_lock);
141 if (pstore_ftrace_enabled) {
142 unregister_ftrace_function(&pstore_ftrace_ops);
Thomas Meyer69596432017-10-07 16:02:21 +0200143 pstore_ftrace_enabled = false;
Geliang Tangee1d2672015-10-20 00:39:03 -0700144 }
145 mutex_unlock(&pstore_ftrace_lock);
146
147 debugfs_remove_recursive(pstore_ftrace_dir);
Anton Vorontsov060287b2012-07-09 17:10:41 -0700148}