Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 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> |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 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> |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 27 | #include <asm/barrier.h> |
| 28 | #include "internal.h" |
| 29 | |
Joel Fernandes | fbccdeb | 2016-10-20 00:34:05 -0700 | [diff] [blame] | 30 | /* This doesn't need to be atomic: speed is chosen over correctness here. */ |
| 31 | static u64 pstore_ftrace_stamp; |
| 32 | |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 33 | static void notrace pstore_ftrace_call(unsigned long ip, |
Anton Vorontsov | ebacfd1 | 2012-11-14 18:48:15 -0800 | [diff] [blame] | 34 | unsigned long parent_ip, |
| 35 | struct ftrace_ops *op, |
| 36 | struct pt_regs *regs) |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 37 | { |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 38 | unsigned long flags; |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 39 | struct pstore_ftrace_record rec = {}; |
| 40 | |
| 41 | if (unlikely(oops_in_progress)) |
| 42 | return; |
| 43 | |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 44 | local_irq_save(flags); |
| 45 | |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 46 | rec.ip = ip; |
| 47 | rec.parent_ip = parent_ip; |
Joel Fernandes | fbccdeb | 2016-10-20 00:34:05 -0700 | [diff] [blame] | 48 | pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++); |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 49 | pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id()); |
| 50 | psinfo->write_buf(PSTORE_TYPE_FTRACE, 0, NULL, 0, (void *)&rec, |
Aruna Balakrishnaiah | 1d8b368 | 2013-07-02 11:06:54 +0530 | [diff] [blame] | 51 | 0, sizeof(rec), psinfo); |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 52 | |
| 53 | local_irq_restore(flags); |
| 54 | } |
| 55 | |
| 56 | static struct ftrace_ops pstore_ftrace_ops __read_mostly = { |
| 57 | .func = pstore_ftrace_call, |
| 58 | }; |
| 59 | |
| 60 | static DEFINE_MUTEX(pstore_ftrace_lock); |
| 61 | static bool pstore_ftrace_enabled; |
| 62 | |
| 63 | static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf, |
| 64 | size_t count, loff_t *ppos) |
| 65 | { |
| 66 | u8 on; |
| 67 | ssize_t ret; |
| 68 | |
| 69 | ret = kstrtou8_from_user(buf, count, 2, &on); |
| 70 | if (ret) |
| 71 | return ret; |
| 72 | |
| 73 | mutex_lock(&pstore_ftrace_lock); |
| 74 | |
| 75 | if (!on ^ pstore_ftrace_enabled) |
| 76 | goto out; |
| 77 | |
Joel Fernandes | 7a0032f | 2016-11-15 12:31:21 -0800 | [diff] [blame] | 78 | if (on) { |
| 79 | ftrace_ops_set_global_filter(&pstore_ftrace_ops); |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 80 | ret = register_ftrace_function(&pstore_ftrace_ops); |
Joel Fernandes | 7a0032f | 2016-11-15 12:31:21 -0800 | [diff] [blame] | 81 | } else { |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 82 | ret = unregister_ftrace_function(&pstore_ftrace_ops); |
Joel Fernandes | 7a0032f | 2016-11-15 12:31:21 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 85 | if (ret) { |
| 86 | pr_err("%s: unable to %sregister ftrace ops: %zd\n", |
| 87 | __func__, on ? "" : "un", ret); |
| 88 | goto err; |
| 89 | } |
| 90 | |
| 91 | pstore_ftrace_enabled = on; |
| 92 | out: |
| 93 | ret = count; |
| 94 | err: |
| 95 | mutex_unlock(&pstore_ftrace_lock); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf, |
| 101 | size_t count, loff_t *ppos) |
| 102 | { |
| 103 | char val[] = { '0' + pstore_ftrace_enabled, '\n' }; |
| 104 | |
| 105 | return simple_read_from_buffer(buf, count, ppos, val, sizeof(val)); |
| 106 | } |
| 107 | |
| 108 | static const struct file_operations pstore_knob_fops = { |
| 109 | .open = simple_open, |
| 110 | .read = pstore_ftrace_knob_read, |
| 111 | .write = pstore_ftrace_knob_write, |
| 112 | }; |
| 113 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 114 | static struct dentry *pstore_ftrace_dir; |
| 115 | |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 116 | void pstore_register_ftrace(void) |
| 117 | { |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 118 | struct dentry *file; |
| 119 | |
| 120 | if (!psinfo->write_buf) |
| 121 | return; |
| 122 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 123 | pstore_ftrace_dir = debugfs_create_dir("pstore", NULL); |
| 124 | if (!pstore_ftrace_dir) { |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 125 | pr_err("%s: unable to create pstore directory\n", __func__); |
| 126 | return; |
| 127 | } |
| 128 | |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 129 | file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir, |
| 130 | NULL, &pstore_knob_fops); |
Anton Vorontsov | 65f8c95 | 2012-07-17 14:26:15 -0700 | [diff] [blame] | 131 | if (!file) { |
| 132 | pr_err("%s: unable to create record_ftrace file\n", __func__); |
| 133 | goto err_file; |
| 134 | } |
| 135 | |
| 136 | return; |
| 137 | err_file: |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 138 | debugfs_remove(pstore_ftrace_dir); |
| 139 | } |
| 140 | |
| 141 | void pstore_unregister_ftrace(void) |
| 142 | { |
| 143 | mutex_lock(&pstore_ftrace_lock); |
| 144 | if (pstore_ftrace_enabled) { |
| 145 | unregister_ftrace_function(&pstore_ftrace_ops); |
| 146 | pstore_ftrace_enabled = 0; |
| 147 | } |
| 148 | mutex_unlock(&pstore_ftrace_lock); |
| 149 | |
| 150 | debugfs_remove_recursive(pstore_ftrace_dir); |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 151 | } |