1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Google, Inc.
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>
16 #include <linux/debugfs.h>
17 #include <linux/err.h>
18 #include <linux/cache.h>
19 #include <asm/barrier.h>
22 /* This doesn't need to be atomic: speed is chosen over correctness here. */
23 static u64 pstore_ftrace_stamp
;
25 static void notrace
pstore_ftrace_call(unsigned long ip
,
26 unsigned long parent_ip
,
27 struct ftrace_ops
*op
,
31 struct pstore_ftrace_record rec
= {};
32 struct pstore_record record
= {
33 .type
= PSTORE_TYPE_FTRACE
,
39 if (unlikely(oops_in_progress
))
42 local_irq_save(flags
);
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
);
50 local_irq_restore(flags
);
53 static struct ftrace_ops pstore_ftrace_ops __read_mostly
= {
54 .func
= pstore_ftrace_call
,
57 static DEFINE_MUTEX(pstore_ftrace_lock
);
58 static bool pstore_ftrace_enabled
;
60 static ssize_t
pstore_ftrace_knob_write(struct file
*f
, const char __user
*buf
,
61 size_t count
, loff_t
*ppos
)
66 ret
= kstrtou8_from_user(buf
, count
, 2, &on
);
70 mutex_lock(&pstore_ftrace_lock
);
72 if (!on
^ pstore_ftrace_enabled
)
76 ftrace_ops_set_global_filter(&pstore_ftrace_ops
);
77 ret
= register_ftrace_function(&pstore_ftrace_ops
);
79 ret
= unregister_ftrace_function(&pstore_ftrace_ops
);
83 pr_err("%s: unable to %sregister ftrace ops: %zd\n",
84 __func__
, on
? "" : "un", ret
);
88 pstore_ftrace_enabled
= on
;
92 mutex_unlock(&pstore_ftrace_lock
);
97 static ssize_t
pstore_ftrace_knob_read(struct file
*f
, char __user
*buf
,
98 size_t count
, loff_t
*ppos
)
100 char val
[] = { '0' + pstore_ftrace_enabled
, '\n' };
102 return simple_read_from_buffer(buf
, count
, ppos
, val
, sizeof(val
));
105 static const struct file_operations pstore_knob_fops
= {
107 .read
= pstore_ftrace_knob_read
,
108 .write
= pstore_ftrace_knob_write
,
111 static struct dentry
*pstore_ftrace_dir
;
113 void pstore_register_ftrace(void)
118 pstore_ftrace_dir
= debugfs_create_dir("pstore", NULL
);
120 debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir
, NULL
,
124 void pstore_unregister_ftrace(void)
126 mutex_lock(&pstore_ftrace_lock
);
127 if (pstore_ftrace_enabled
) {
128 unregister_ftrace_function(&pstore_ftrace_ops
);
129 pstore_ftrace_enabled
= false;
131 mutex_unlock(&pstore_ftrace_lock
);
133 debugfs_remove_recursive(pstore_ftrace_dir
);