tracing/ring-buffer: Add last_boot_info file to boot instance
authorSteven Rostedt (Google) <rostedt@goodmis.org>
Wed, 12 Jun 2024 23:19:44 +0000 (19:19 -0400)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Fri, 14 Jun 2024 16:28:22 +0000 (12:28 -0400)
If an instance is mapped to memory on boot up, create a new file called
"last_boot_info" that will hold information that can be used to properly
parse the raw data in the ring buffer.

It will export the delta of the addresses for text and data from what it
was from the last boot. It does not expose actually addresses (unless you
knew what the actual address was from the last boot).

The output will look like:

 # cat last_boot_info
 text delta: -268435456
 data delta: -268435456

The text and data are kept separate in case they are ever made different.

Link: https://lkml.kernel.org/r/20240612232026.658680738@goodmis.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Vincent Donnefort <vdonnefort@google.com>
Cc: Joel Fernandes <joel@joelfernandes.org>
Cc: Daniel Bristot de Oliveira <bristot@redhat.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vineeth Pillai <vineeth@bitbyteword.org>
Cc: Youssef Esmat <youssefesmat@google.com>
Cc: Beau Belgrave <beaub@linux.microsoft.com>
Cc: Alexander Graf <graf@amazon.com>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: David Howells <dhowells@redhat.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Ross Zwisler <zwisler@google.com>
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
include/linux/ring_buffer.h
kernel/trace/ring_buffer.c
kernel/trace/trace.c
kernel/trace/trace.h

index a50b0223b1d32c75cfe1cc5bfbf29de69a00e38e..55de3798a9b98043aad1e9874c32dca0456381aa 100644 (file)
@@ -94,6 +94,9 @@ struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flag
                                               unsigned long range_size,
                                               struct lock_class_key *key);
 
+bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, long *text,
+                                long *data);
+
 /*
  * Because the ring buffer is generic, if other users of the ring buffer get
  * traced by ftrace, it can produce lockdep warnings. We need to keep each
index 8c287430411d9812aed033cd60bacce6ab3876e2..f3d772461a60fbcb83f06a498ae7eba4c644a81f 100644 (file)
@@ -2396,6 +2396,29 @@ struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flag
        return alloc_buffer(size, flags, order, start, start + range_size, key);
 }
 
+/**
+ * ring_buffer_last_boot_delta - return the delta offset from last boot
+ * @buffer: The buffer to return the delta from
+ * @text: Return text delta
+ * @data: Return data delta
+ *
+ * Returns: The true if the delta is non zero
+ */
+bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, long *text,
+                                long *data)
+{
+       if (!buffer)
+               return false;
+
+       if (!buffer->last_text_delta)
+               return false;
+
+       *text = buffer->last_text_delta;
+       *data = buffer->last_data_delta;
+
+       return true;
+}
+
 /**
  * ring_buffer_free - free a ring buffer.
  * @buffer: the buffer to free.
index dfde26aa321181140290c763efe97a4391ee132b..dc4eee33d920c9d4140b212a9d24e00767e1fda4 100644 (file)
@@ -6041,6 +6041,18 @@ out:
        return ret;
 }
 
+static void update_last_data(struct trace_array *tr)
+{
+       if (!tr->text_delta && !tr->data_delta)
+               return;
+
+       /* Clear old data */
+       tracing_reset_online_cpus(&tr->array_buffer);
+
+       /* Using current data now */
+       tr->text_delta = 0;
+       tr->data_delta = 0;
+}
 
 /**
  * tracing_update_buffers - used by tracing facility to expand ring buffers
@@ -6058,6 +6070,9 @@ int tracing_update_buffers(struct trace_array *tr)
        int ret = 0;
 
        mutex_lock(&trace_types_lock);
+
+       update_last_data(tr);
+
        if (!tr->ring_buffer_expanded)
                ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
                                                RING_BUFFER_ALL_CPUS);
@@ -6113,6 +6128,8 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf)
 
        mutex_lock(&trace_types_lock);
 
+       update_last_data(tr);
+
        if (!tr->ring_buffer_expanded) {
                ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
                                                RING_BUFFER_ALL_CPUS);
@@ -6860,6 +6877,21 @@ tracing_total_entries_read(struct file *filp, char __user *ubuf,
        return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
 }
 
+static ssize_t
+tracing_last_boot_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
+{
+       struct trace_array *tr = filp->private_data;
+       struct seq_buf seq;
+       char buf[64];
+
+       seq_buf_init(&seq, buf, 64);
+
+       seq_buf_printf(&seq, "text delta:\t%ld\n", tr->text_delta);
+       seq_buf_printf(&seq, "data delta:\t%ld\n", tr->data_delta);
+
+       return simple_read_from_buffer(ubuf, cnt, ppos, buf, seq_buf_used(&seq));
+}
+
 static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
 {
        struct trace_array *tr = inode->i_private;
@@ -7499,6 +7531,13 @@ static const struct file_operations trace_time_stamp_mode_fops = {
        .release        = tracing_single_release_tr,
 };
 
+static const struct file_operations last_boot_fops = {
+       .open           = tracing_open_generic_tr,
+       .read           = tracing_last_boot_read,
+       .llseek         = generic_file_llseek,
+       .release        = tracing_release_generic_tr,
+};
+
 #ifdef CONFIG_TRACER_SNAPSHOT
 static const struct file_operations snapshot_fops = {
        .open           = tracing_snapshot_open,
@@ -9242,6 +9281,9 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size
                buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
                                                      tr->range_addr_start,
                                                      tr->range_addr_size);
+
+               ring_buffer_last_boot_delta(buf->buffer,
+                                           &tr->text_delta, &tr->data_delta);
                /*
                 * This is basically the same as a mapped buffer,
                 * with the same restrictions.
@@ -9751,7 +9793,10 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
                MEM_FAIL(1, "Could not allocate function filter files");
 
 #ifdef CONFIG_TRACER_SNAPSHOT
-       if (!tr->range_addr_start) {
+       if (tr->range_addr_start) {
+               trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer,
+                                 tr, &last_boot_fops);
+       } else {
                trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
                                  tr, &snapshot_fops);
        }
index 3e56d3b222126204c30b535700932930f61c0856..3dc5c8f14ce94fad29b8714d272d3e111fa9a7ad 100644 (file)
@@ -347,6 +347,8 @@ struct trace_array {
        unsigned int            mapped;
        unsigned long           range_addr_start;
        unsigned long           range_addr_size;
+       long                    text_delta;
+       long                    data_delta;
 
        struct trace_pid_list   __rcu *filtered_pids;
        struct trace_pid_list   __rcu *filtered_no_pids;