Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2012  Google, Inc.
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/compiler.h>
0008 #include <linux/irqflags.h>
0009 #include <linux/percpu.h>
0010 #include <linux/smp.h>
0011 #include <linux/atomic.h>
0012 #include <linux/types.h>
0013 #include <linux/mutex.h>
0014 #include <linux/ftrace.h>
0015 #include <linux/fs.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/err.h>
0018 #include <linux/cache.h>
0019 #include <linux/slab.h>
0020 #include <asm/barrier.h>
0021 #include "internal.h"
0022 
0023 /* This doesn't need to be atomic: speed is chosen over correctness here. */
0024 static u64 pstore_ftrace_stamp;
0025 
0026 static void notrace pstore_ftrace_call(unsigned long ip,
0027                        unsigned long parent_ip,
0028                        struct ftrace_ops *op,
0029                        struct ftrace_regs *fregs)
0030 {
0031     int bit;
0032     unsigned long flags;
0033     struct pstore_ftrace_record rec = {};
0034     struct pstore_record record = {
0035         .type = PSTORE_TYPE_FTRACE,
0036         .buf = (char *)&rec,
0037         .size = sizeof(rec),
0038         .psi = psinfo,
0039     };
0040 
0041     if (unlikely(oops_in_progress))
0042         return;
0043 
0044     bit = ftrace_test_recursion_trylock(ip, parent_ip);
0045     if (bit < 0)
0046         return;
0047 
0048     local_irq_save(flags);
0049 
0050     rec.ip = ip;
0051     rec.parent_ip = parent_ip;
0052     pstore_ftrace_write_timestamp(&rec, pstore_ftrace_stamp++);
0053     pstore_ftrace_encode_cpu(&rec, raw_smp_processor_id());
0054     psinfo->write(&record);
0055 
0056     local_irq_restore(flags);
0057     ftrace_test_recursion_unlock(bit);
0058 }
0059 
0060 static struct ftrace_ops pstore_ftrace_ops __read_mostly = {
0061     .func   = pstore_ftrace_call,
0062 };
0063 
0064 static DEFINE_MUTEX(pstore_ftrace_lock);
0065 static bool pstore_ftrace_enabled;
0066 
0067 static int pstore_set_ftrace_enabled(bool on)
0068 {
0069     ssize_t ret;
0070 
0071     if (on == pstore_ftrace_enabled)
0072         return 0;
0073 
0074     if (on) {
0075         ftrace_ops_set_global_filter(&pstore_ftrace_ops);
0076         ret = register_ftrace_function(&pstore_ftrace_ops);
0077     } else {
0078         ret = unregister_ftrace_function(&pstore_ftrace_ops);
0079     }
0080 
0081     if (ret) {
0082         pr_err("%s: unable to %sregister ftrace ops: %zd\n",
0083                __func__, on ? "" : "un", ret);
0084     } else {
0085         pstore_ftrace_enabled = on;
0086     }
0087 
0088     return ret;
0089 }
0090 
0091 static ssize_t pstore_ftrace_knob_write(struct file *f, const char __user *buf,
0092                     size_t count, loff_t *ppos)
0093 {
0094     u8 on;
0095     ssize_t ret;
0096 
0097     ret = kstrtou8_from_user(buf, count, 2, &on);
0098     if (ret)
0099         return ret;
0100 
0101     mutex_lock(&pstore_ftrace_lock);
0102     ret = pstore_set_ftrace_enabled(on);
0103     mutex_unlock(&pstore_ftrace_lock);
0104 
0105     if (ret == 0)
0106         ret = count;
0107 
0108     return ret;
0109 }
0110 
0111 static ssize_t pstore_ftrace_knob_read(struct file *f, char __user *buf,
0112                        size_t count, loff_t *ppos)
0113 {
0114     char val[] = { '0' + pstore_ftrace_enabled, '\n' };
0115 
0116     return simple_read_from_buffer(buf, count, ppos, val, sizeof(val));
0117 }
0118 
0119 static const struct file_operations pstore_knob_fops = {
0120     .open   = simple_open,
0121     .read   = pstore_ftrace_knob_read,
0122     .write  = pstore_ftrace_knob_write,
0123 };
0124 
0125 static struct dentry *pstore_ftrace_dir;
0126 
0127 static bool record_ftrace;
0128 module_param(record_ftrace, bool, 0400);
0129 MODULE_PARM_DESC(record_ftrace,
0130          "enable ftrace recording immediately (default: off)");
0131 
0132 void pstore_register_ftrace(void)
0133 {
0134     if (!psinfo->write)
0135         return;
0136 
0137     pstore_ftrace_dir = debugfs_create_dir("pstore", NULL);
0138 
0139     pstore_set_ftrace_enabled(record_ftrace);
0140 
0141     debugfs_create_file("record_ftrace", 0600, pstore_ftrace_dir, NULL,
0142                 &pstore_knob_fops);
0143 }
0144 
0145 void pstore_unregister_ftrace(void)
0146 {
0147     mutex_lock(&pstore_ftrace_lock);
0148     if (pstore_ftrace_enabled) {
0149         unregister_ftrace_function(&pstore_ftrace_ops);
0150         pstore_ftrace_enabled = false;
0151     }
0152     mutex_unlock(&pstore_ftrace_lock);
0153 
0154     debugfs_remove_recursive(pstore_ftrace_dir);
0155 }
0156 
0157 ssize_t pstore_ftrace_combine_log(char **dest_log, size_t *dest_log_size,
0158                   const char *src_log, size_t src_log_size)
0159 {
0160     size_t dest_size, src_size, total, dest_off, src_off;
0161     size_t dest_idx = 0, src_idx = 0, merged_idx = 0;
0162     void *merged_buf;
0163     struct pstore_ftrace_record *drec, *srec, *mrec;
0164     size_t record_size = sizeof(struct pstore_ftrace_record);
0165 
0166     dest_off = *dest_log_size % record_size;
0167     dest_size = *dest_log_size - dest_off;
0168 
0169     src_off = src_log_size % record_size;
0170     src_size = src_log_size - src_off;
0171 
0172     total = dest_size + src_size;
0173     merged_buf = kmalloc(total, GFP_KERNEL);
0174     if (!merged_buf)
0175         return -ENOMEM;
0176 
0177     drec = (struct pstore_ftrace_record *)(*dest_log + dest_off);
0178     srec = (struct pstore_ftrace_record *)(src_log + src_off);
0179     mrec = (struct pstore_ftrace_record *)(merged_buf);
0180 
0181     while (dest_size > 0 && src_size > 0) {
0182         if (pstore_ftrace_read_timestamp(&drec[dest_idx]) <
0183             pstore_ftrace_read_timestamp(&srec[src_idx])) {
0184             mrec[merged_idx++] = drec[dest_idx++];
0185             dest_size -= record_size;
0186         } else {
0187             mrec[merged_idx++] = srec[src_idx++];
0188             src_size -= record_size;
0189         }
0190     }
0191 
0192     while (dest_size > 0) {
0193         mrec[merged_idx++] = drec[dest_idx++];
0194         dest_size -= record_size;
0195     }
0196 
0197     while (src_size > 0) {
0198         mrec[merged_idx++] = srec[src_idx++];
0199         src_size -= record_size;
0200     }
0201 
0202     kfree(*dest_log);
0203     *dest_log = merged_buf;
0204     *dest_log_size = total;
0205 
0206     return 0;
0207 }
0208 EXPORT_SYMBOL_GPL(pstore_ftrace_combine_log);