Skip to content

Commit 2124de7

Browse files
committed
tracing: Implement creating an instance based on a given memory region
Allow for creating a new instance by passing in an address and size to map the ring buffer for the instance to. This will allow features like a pstore memory mapped region to be used for an tracing instance ring buffer that can be retrieved from one boot to the next. Link: https://lkml.kernel.org/r/[email protected] Cc: Masami Hiramatsu <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Vincent Donnefort <[email protected]> Cc: Joel Fernandes <[email protected]> Cc: Daniel Bristot de Oliveira <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vineeth Pillai <[email protected]> Cc: Youssef Esmat <[email protected]> Cc: Beau Belgrave <[email protected]> Cc: Alexander Graf <[email protected]> Cc: Baoquan He <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: "Paul E. McKenney" <[email protected]> Cc: David Howells <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Tony Luck <[email protected]> Cc: Guenter Roeck <[email protected]> Cc: Ross Zwisler <[email protected]> Cc: Kees Cook <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent b14d032 commit 2124de7

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

kernel/trace/trace.c

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,6 +4921,11 @@ static int tracing_open(struct inode *inode, struct file *file)
49214921
static bool
49224922
trace_ok_for_array(struct tracer *t, struct trace_array *tr)
49234923
{
4924+
#ifdef CONFIG_TRACER_SNAPSHOT
4925+
/* arrays with mapped buffer range do not have snapshots */
4926+
if (tr->range_addr_start && t->use_max_tr)
4927+
return false;
4928+
#endif
49244929
return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
49254930
}
49264931

@@ -8664,11 +8669,13 @@ tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
86648669
tr, cpu, &tracing_entries_fops);
86658670

86668671
#ifdef CONFIG_TRACER_SNAPSHOT
8667-
trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
8668-
tr, cpu, &snapshot_fops);
8672+
if (!tr->range_addr_start) {
8673+
trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
8674+
tr, cpu, &snapshot_fops);
86698675

8670-
trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
8671-
tr, cpu, &snapshot_raw_fops);
8676+
trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
8677+
tr, cpu, &snapshot_raw_fops);
8678+
}
86728679
#endif
86738680
}
86748681

@@ -9205,7 +9212,18 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size
92059212

92069213
buf->tr = tr;
92079214

9208-
buf->buffer = ring_buffer_alloc(size, rb_flags);
9215+
if (tr->range_addr_start && tr->range_addr_size) {
9216+
buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
9217+
tr->range_addr_start,
9218+
tr->range_addr_size);
9219+
/*
9220+
* This is basically the same as a mapped buffer,
9221+
* with the same restrictions.
9222+
*/
9223+
tr->mapped++;
9224+
} else {
9225+
buf->buffer = ring_buffer_alloc(size, rb_flags);
9226+
}
92099227
if (!buf->buffer)
92109228
return -ENOMEM;
92119229

@@ -9242,6 +9260,10 @@ static int allocate_trace_buffers(struct trace_array *tr, int size)
92429260
return ret;
92439261

92449262
#ifdef CONFIG_TRACER_MAX_TRACE
9263+
/* Fix mapped buffer trace arrays do not have snapshot buffers */
9264+
if (tr->range_addr_start)
9265+
return 0;
9266+
92459267
ret = allocate_trace_buffer(tr, &tr->max_buffer,
92469268
allocate_snapshot ? size : 1);
92479269
if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
@@ -9342,7 +9364,9 @@ static int trace_array_create_dir(struct trace_array *tr)
93429364
}
93439365

93449366
static struct trace_array *
9345-
trace_array_create_systems(const char *name, const char *systems)
9367+
trace_array_create_systems(const char *name, const char *systems,
9368+
unsigned long range_addr_start,
9369+
unsigned long range_addr_size)
93469370
{
93479371
struct trace_array *tr;
93489372
int ret;
@@ -9368,6 +9392,10 @@ trace_array_create_systems(const char *name, const char *systems)
93689392
goto out_free_tr;
93699393
}
93709394

9395+
/* Only for boot up memory mapped ring buffers */
9396+
tr->range_addr_start = range_addr_start;
9397+
tr->range_addr_size = range_addr_size;
9398+
93719399
tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
93729400

93739401
cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
@@ -9425,7 +9453,7 @@ trace_array_create_systems(const char *name, const char *systems)
94259453

94269454
static struct trace_array *trace_array_create(const char *name)
94279455
{
9428-
return trace_array_create_systems(name, NULL);
9456+
return trace_array_create_systems(name, NULL, 0, 0);
94299457
}
94309458

94319459
static int instance_mkdir(const char *name)
@@ -9479,7 +9507,7 @@ struct trace_array *trace_array_get_by_name(const char *name, const char *system
94799507
goto out_unlock;
94809508
}
94819509

9482-
tr = trace_array_create_systems(name, systems);
9510+
tr = trace_array_create_systems(name, systems, 0, 0);
94839511

94849512
if (IS_ERR(tr))
94859513
tr = NULL;
@@ -9672,8 +9700,10 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
96729700
MEM_FAIL(1, "Could not allocate function filter files");
96739701

96749702
#ifdef CONFIG_TRACER_SNAPSHOT
9675-
trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
9676-
tr, &snapshot_fops);
9703+
if (!tr->range_addr_start) {
9704+
trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
9705+
tr, &snapshot_fops);
9706+
}
96779707
#endif
96789708

96799709
trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer,

kernel/trace/trace.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,18 @@ struct trace_array {
336336
bool allocated_snapshot;
337337
spinlock_t snapshot_trigger_lock;
338338
unsigned int snapshot;
339-
unsigned int mapped;
340339
unsigned long max_latency;
341340
#ifdef CONFIG_FSNOTIFY
342341
struct dentry *d_max_latency;
343342
struct work_struct fsnotify_work;
344343
struct irq_work fsnotify_irqwork;
345344
#endif
346345
#endif
346+
/* The below is for memory mapped ring buffer */
347+
unsigned int mapped;
348+
unsigned long range_addr_start;
349+
unsigned long range_addr_size;
350+
347351
struct trace_pid_list __rcu *filtered_pids;
348352
struct trace_pid_list __rcu *filtered_no_pids;
349353
/*

0 commit comments

Comments
 (0)