Linux Audio

Check our new training course

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 <linux/slab.h>
 20#include <asm/barrier.h>
 21#include "internal.h"
 22
 23/* This doesn't need to be atomic: speed is chosen over correctness here. */
 24static u64 pstore_ftrace_stamp;
 25
 26static void notrace pstore_ftrace_call(unsigned long ip,
 27				       unsigned long parent_ip,
 28				       struct ftrace_ops *op,
 29				       struct pt_regs *regs)
 30{
 31	unsigned long flags;
 32	struct pstore_ftrace_record rec = {};
 33	struct pstore_record record = {
 34		.type = PSTORE_TYPE_FTRACE,
 35		.buf = (char *)&rec,
 36		.size = sizeof(rec),
 37		.psi = psinfo,
 38	};
 39
 40	if (unlikely(oops_in_progress))
 41		return;
 42
 43	local_irq_save(flags);
 44
 45	rec.ip = ip;
 46	rec.parent_ip = parent_ip;
 47	pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
 48	pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
 49	psinfo->write(&record);
 50
 51	local_irq_restore(flags);
 52}
 53
 54static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
 55	.func	= pstore_ftrace_call,
 56};
 57
 58static DEFINE_MUTEX(pstore_ftrace_lock);
 59static bool pstore_ftrace_enabled;
 60
 61static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
 62					size_t count, loff_t *ppos)
 63{
 64	u8 on;
 65	ssize_t ret;
 66
 67	ret = kstrtou8_from_user(buf, count, 2, &on);
 68	if (ret)
 69		return ret;
 70
 71	mutex_lock(&pstore_ftrace_lock);
 72
 73	if (!on ^ pstore_ftrace_enabled)
 74		goto out;
 75
 76	if (on) {
 77		ftrace_ops_set_global_filter(&pstore_ftrace_ops);
 78		ret = register_ftrace_function(&pstore_ftrace_ops);
 79	} else {
 80		ret = unregister_ftrace_function(&pstore_ftrace_ops);
 81	}
 82
 83	if (ret) {
 84		pr_err("%s: unable to %sregister ftrace ops: %zd\n",
 85		       __func__, on ? "" : "un", ret);
 86		goto err;
 87	}
 88
 89	pstore_ftrace_enabled = on;
 90out:
 91	ret = count;
 92err:
 93	mutex_unlock(&pstore_ftrace_lock);
 94
 95	return ret;
 96}
 97
 98static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
 99				       size_t count, loff_t *ppos)
100{
101	char val[] = { '0' + pstore_ftrace_enabled, '\n' };
102
103	return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
104}
105
106static const struct file_operations pstore_knob_fops = {
107	.open	= simple_open,
108	.read	= pstore_ftrace_knob_read,
109	.write	= pstore_ftrace_knob_write,
110};
111
112static struct dentry *pstore_ftrace_dir;
113
114void pstore_register_ftrace(void)
115{
116	if (!psinfo->write)
117		return;
118
119	pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
120
121	debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir, NULL,
122			    &pstore_knob_fops);
123}
124
125void pstore_unregister_ftrace(void)
126{
127	mutex_lock(&pstore_ftrace_lock);
128	if (pstore_ftrace_enabled) {
129		unregister_ftrace_function(&pstore_ftrace_ops);
130		pstore_ftrace_enabled = false;
131	}
132	mutex_unlock(&pstore_ftrace_lock);
133
134	debugfs_remove_recursive(pstore_ftrace_dir);
135}
136
137ssize_t pstore_ftrace_combine_log(char **dest_log, size_t *dest_log_size,
138				  const char *src_log, size_t src_log_size)
139{
140	size_t dest_size, src_size, total, dest_off, src_off;
141	size_t dest_idx = 0, src_idx = 0, merged_idx = 0;
142	void *merged_buf;
143	struct pstore_ftrace_record *drec, *srec, *mrec;
144	size_t record_size = sizeof(struct pstore_ftrace_record);
145
146	dest_off = *dest_log_size % record_size;
147	dest_size = *dest_log_size - dest_off;
148
149	src_off = src_log_size % record_size;
150	src_size = src_log_size - src_off;
151
152	total = dest_size + src_size;
153	merged_buf = kmalloc(total, GFP_KERNEL);
154	if (!merged_buf)
155		return -ENOMEM;
156
157	drec = (struct pstore_ftrace_record *)(*dest_log + dest_off);
158	srec = (struct pstore_ftrace_record *)(src_log + src_off);
159	mrec = (struct pstore_ftrace_record *)(merged_buf);
160
161	while (dest_size > 0 && src_size > 0) {
162		if (pstore_ftrace_read_timestamp(&drec[dest_idx]) <
163		    pstore_ftrace_read_timestamp(&srec[src_idx])) {
164			mrec[merged_idx++] = drec[dest_idx++];
165			dest_size -= record_size;
166		} else {
167			mrec[merged_idx++] = srec[src_idx++];
168			src_size -= record_size;
169		}
170	}
171
172	while (dest_size > 0) {
173		mrec[merged_idx++] = drec[dest_idx++];
174		dest_size -= record_size;
175	}
176
177	while (src_size > 0) {
178		mrec[merged_idx++] = srec[src_idx++];
179		src_size -= record_size;
180	}
181
182	kfree(*dest_log);
183	*dest_log = merged_buf;
184	*dest_log_size = total;
185
186	return 0;
187}
188EXPORT_SYMBOL_GPL(pstore_ftrace_combine_log);
  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
 30/* This doesn't need to be atomic: speed is chosen over correctness here. */
 31static u64 pstore_ftrace_stamp;
 32
 33static void notrace pstore_ftrace_call(unsigned long ip,
 34				       unsigned long parent_ip,
 35				       struct ftrace_ops *op,
 36				       struct pt_regs *regs)
 37{
 38	unsigned long flags;
 39	struct pstore_ftrace_record rec = {};
 40
 41	if (unlikely(oops_in_progress))
 42		return;
 43
 44	local_irq_save(flags);
 45
 46	rec.ip = ip;
 47	rec.parent_ip = parent_ip;
 48	pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
 49	pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
 50	psinfo->write_buf(PSTORE_TYPE_FTRACE, 0, NULL, 0, (void *)&rec,
 51			  0, sizeof(rec), psinfo);
 52
 53	local_irq_restore(flags);
 54}
 55
 56static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
 57	.func	= pstore_ftrace_call,
 58};
 59
 60static DEFINE_MUTEX(pstore_ftrace_lock);
 61static bool pstore_ftrace_enabled;
 62
 63static 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
 78	if (on) {
 79		ftrace_ops_set_global_filter(&pstore_ftrace_ops);
 80		ret = register_ftrace_function(&pstore_ftrace_ops);
 81	} else {
 82		ret = unregister_ftrace_function(&pstore_ftrace_ops);
 83	}
 84
 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;
 92out:
 93	ret = count;
 94err:
 95	mutex_unlock(&pstore_ftrace_lock);
 96
 97	return ret;
 98}
 99
100static 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
108static 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
114static struct dentry *pstore_ftrace_dir;
115
116void pstore_register_ftrace(void)
117{
118	struct dentry *file;
119
120	if (!psinfo->write_buf)
121		return;
122
123	pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
124	if (!pstore_ftrace_dir) {
125		pr_err("%s: unable to create pstore directory\n", __func__);
126		return;
127	}
128
129	file = debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir,
130				   NULL, &pstore_knob_fops);
131	if (!file) {
132		pr_err("%s: unable to create record_ftrace file\n", __func__);
133		goto err_file;
134	}
135
136	return;
137err_file:
138	debugfs_remove(pstore_ftrace_dir);
139}
140
141void 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);
151}