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 <linux/slab.h>
20 #include <asm/barrier.h>
23 /* This doesn't need to be atomic: speed is chosen over correctness here. */
24 static u64 pstore_ftrace_stamp
;
26 static void notrace
pstore_ftrace_call(unsigned long ip
,
27 unsigned long parent_ip
,
28 struct ftrace_ops
*op
,
29 struct ftrace_regs
*fregs
)
33 struct pstore_ftrace_record rec
= {};
34 struct pstore_record record
= {
35 .type
= PSTORE_TYPE_FTRACE
,
41 if (unlikely(oops_in_progress
))
44 bit
= ftrace_test_recursion_trylock(ip
, parent_ip
);
48 local_irq_save(flags
);
51 rec
.parent_ip
= parent_ip
;
52 pstore_ftrace_write_timestamp(&rec
, pstore_ftrace_stamp
++);
53 pstore_ftrace_encode_cpu(&rec
, raw_smp_processor_id());
54 psinfo
->write(&record
);
56 local_irq_restore(flags
);
57 ftrace_test_recursion_unlock(bit
);
60 static struct ftrace_ops pstore_ftrace_ops __read_mostly
= {
61 .func
= pstore_ftrace_call
,
64 static DEFINE_MUTEX(pstore_ftrace_lock
);
65 static bool pstore_ftrace_enabled
;
67 static ssize_t
pstore_ftrace_knob_write(struct file
*f
, const char __user
*buf
,
68 size_t count
, loff_t
*ppos
)
73 ret
= kstrtou8_from_user(buf
, count
, 2, &on
);
77 mutex_lock(&pstore_ftrace_lock
);
79 if (!on
^ pstore_ftrace_enabled
)
83 ftrace_ops_set_global_filter(&pstore_ftrace_ops
);
84 ret
= register_ftrace_function(&pstore_ftrace_ops
);
86 ret
= unregister_ftrace_function(&pstore_ftrace_ops
);
90 pr_err("%s: unable to %sregister ftrace ops: %zd\n",
91 __func__
, on
? "" : "un", ret
);
95 pstore_ftrace_enabled
= on
;
99 mutex_unlock(&pstore_ftrace_lock
);
104 static ssize_t
pstore_ftrace_knob_read(struct file
*f
, char __user
*buf
,
105 size_t count
, loff_t
*ppos
)
107 char val
[] = { '0' + pstore_ftrace_enabled
, '\n' };
109 return simple_read_from_buffer(buf
, count
, ppos
, val
, sizeof(val
));
112 static const struct file_operations pstore_knob_fops
= {
114 .read
= pstore_ftrace_knob_read
,
115 .write
= pstore_ftrace_knob_write
,
118 static struct dentry
*pstore_ftrace_dir
;
120 void pstore_register_ftrace(void)
125 pstore_ftrace_dir
= debugfs_create_dir("pstore", NULL
);
127 debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir
, NULL
,
131 void pstore_unregister_ftrace(void)
133 mutex_lock(&pstore_ftrace_lock
);
134 if (pstore_ftrace_enabled
) {
135 unregister_ftrace_function(&pstore_ftrace_ops
);
136 pstore_ftrace_enabled
= false;
138 mutex_unlock(&pstore_ftrace_lock
);
140 debugfs_remove_recursive(pstore_ftrace_dir
);
143 ssize_t
pstore_ftrace_combine_log(char **dest_log
, size_t *dest_log_size
,
144 const char *src_log
, size_t src_log_size
)
146 size_t dest_size
, src_size
, total
, dest_off
, src_off
;
147 size_t dest_idx
= 0, src_idx
= 0, merged_idx
= 0;
149 struct pstore_ftrace_record
*drec
, *srec
, *mrec
;
150 size_t record_size
= sizeof(struct pstore_ftrace_record
);
152 dest_off
= *dest_log_size
% record_size
;
153 dest_size
= *dest_log_size
- dest_off
;
155 src_off
= src_log_size
% record_size
;
156 src_size
= src_log_size
- src_off
;
158 total
= dest_size
+ src_size
;
159 merged_buf
= kmalloc(total
, GFP_KERNEL
);
163 drec
= (struct pstore_ftrace_record
*)(*dest_log
+ dest_off
);
164 srec
= (struct pstore_ftrace_record
*)(src_log
+ src_off
);
165 mrec
= (struct pstore_ftrace_record
*)(merged_buf
);
167 while (dest_size
> 0 && src_size
> 0) {
168 if (pstore_ftrace_read_timestamp(&drec
[dest_idx
]) <
169 pstore_ftrace_read_timestamp(&srec
[src_idx
])) {
170 mrec
[merged_idx
++] = drec
[dest_idx
++];
171 dest_size
-= record_size
;
173 mrec
[merged_idx
++] = srec
[src_idx
++];
174 src_size
-= record_size
;
178 while (dest_size
> 0) {
179 mrec
[merged_idx
++] = drec
[dest_idx
++];
180 dest_size
-= record_size
;
183 while (src_size
> 0) {
184 mrec
[merged_idx
++] = srec
[src_idx
++];
185 src_size
-= record_size
;
189 *dest_log
= merged_buf
;
190 *dest_log_size
= total
;
194 EXPORT_SYMBOL_GPL(pstore_ftrace_combine_log
);