Skip to content

Commit b653348

Browse files
committed
tracing: Have persistent trace instances save KASLR offset
There's no reason to save the KASLR offset for the ring buffer itself. That is used by the tracer. Now that the tracer has a way to save data in the persistent memory of the ring buffer, have the tracing infrastructure take care of the saving of the KASLR offset. Cc: Mark Rutland <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Andrew Morton <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 4af0a9c commit b653348

File tree

4 files changed

+46
-57
lines changed

4 files changed

+46
-57
lines changed

include/linux/ring_buffer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flag
9595
unsigned long scratch_size,
9696
struct lock_class_key *key);
9797

98-
bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, unsigned long *kaslr_addr);
9998
void *ring_buffer_meta_scratch(struct trace_buffer *buffer, unsigned int *size);
10099

101100
/*

kernel/trace/ring_buffer.c

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ struct ring_buffer_meta {
5555
};
5656

5757
struct ring_buffer_cpu_meta {
58-
unsigned long kaslr_addr;
5958
unsigned long first_buffer;
6059
unsigned long head_buffer;
6160
unsigned long commit_buffer;
@@ -557,8 +556,6 @@ struct trace_buffer {
557556

558557
struct ring_buffer_meta *meta;
559558

560-
unsigned long kaslr_addr;
561-
562559
unsigned int subbuf_size;
563560
unsigned int subbuf_order;
564561
unsigned int max_data_size;
@@ -1949,15 +1946,6 @@ static void rb_meta_validate_events(struct ring_buffer_per_cpu *cpu_buffer)
19491946
}
19501947
}
19511948

1952-
static void rb_meta_init_text_addr(struct ring_buffer_cpu_meta *meta)
1953-
{
1954-
#ifdef CONFIG_RANDOMIZE_BASE
1955-
meta->kaslr_addr = kaslr_offset();
1956-
#else
1957-
meta->kaslr_addr = 0;
1958-
#endif
1959-
}
1960-
19611949
static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int scratch_size)
19621950
{
19631951
struct ring_buffer_cpu_meta *meta;
@@ -1990,7 +1978,6 @@ static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int sc
19901978
meta->first_buffer += delta;
19911979
meta->head_buffer += delta;
19921980
meta->commit_buffer += delta;
1993-
buffer->kaslr_addr = meta->kaslr_addr;
19941981
continue;
19951982
}
19961983

@@ -2007,7 +1994,6 @@ static void rb_range_meta_init(struct trace_buffer *buffer, int nr_pages, int sc
20071994
subbuf = rb_subbufs_from_meta(meta);
20081995

20091996
meta->first_buffer = (unsigned long)subbuf;
2010-
rb_meta_init_text_addr(meta);
20111997

20121998
/*
20131999
* The buffers[] array holds the order of the sub-buffers
@@ -2549,35 +2535,22 @@ struct trace_buffer *__ring_buffer_alloc_range(unsigned long size, unsigned flag
25492535
scratch_size, key);
25502536
}
25512537

2552-
/**
2553-
* ring_buffer_last_boot_delta - return the delta offset from last boot
2554-
* @buffer: The buffer to return the delta from
2555-
* @text: Return text delta
2556-
* @data: Return data delta
2557-
*
2558-
* Returns: The true if the delta is non zero
2559-
*/
2560-
bool ring_buffer_last_boot_delta(struct trace_buffer *buffer, unsigned long *kaslr_addr)
2561-
{
2562-
if (!buffer)
2563-
return false;
2564-
2565-
if (!buffer->kaslr_addr)
2566-
return false;
2567-
2568-
*kaslr_addr = buffer->kaslr_addr;
2569-
2570-
return true;
2571-
}
2572-
25732538
void *ring_buffer_meta_scratch(struct trace_buffer *buffer, unsigned int *size)
25742539
{
2540+
struct ring_buffer_meta *meta;
2541+
void *ptr;
2542+
25752543
if (!buffer || !buffer->meta)
25762544
return NULL;
25772545

2578-
*size = PAGE_SIZE - sizeof(*buffer->meta);
2546+
meta = buffer->meta;
25792547

2580-
return (void *)buffer->meta + sizeof(*buffer->meta);
2548+
ptr = (void *)ALIGN((unsigned long)meta + sizeof(*meta), sizeof(long));
2549+
2550+
if (size)
2551+
*size = (void *)meta + meta->buffers_offset - ptr;
2552+
2553+
return ptr;
25812554
}
25822555

25832556
/**
@@ -6133,7 +6106,6 @@ static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
61336106
void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
61346107
{
61356108
struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
6136-
struct ring_buffer_cpu_meta *meta;
61376109

61386110
if (!cpumask_test_cpu(cpu, buffer->cpumask))
61396111
return;
@@ -6152,11 +6124,6 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
61526124
atomic_dec(&cpu_buffer->record_disabled);
61536125
atomic_dec(&cpu_buffer->resize_disabled);
61546126

6155-
/* Make sure persistent meta now uses this buffer's addresses */
6156-
meta = rb_range_meta(buffer, 0, cpu_buffer->cpu);
6157-
if (meta)
6158-
rb_meta_init_text_addr(meta);
6159-
61606127
mutex_unlock(&buffer->mutex);
61616128
}
61626129
EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
@@ -6171,7 +6138,6 @@ EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
61716138
void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
61726139
{
61736140
struct ring_buffer_per_cpu *cpu_buffer;
6174-
struct ring_buffer_cpu_meta *meta;
61756141
int cpu;
61766142

61776143
/* prevent another thread from changing buffer sizes */
@@ -6199,11 +6165,6 @@ void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
61996165

62006166
reset_disabled_cpu_buffer(cpu_buffer);
62016167

6202-
/* Make sure persistent meta now uses this buffer's addresses */
6203-
meta = rb_range_meta(buffer, 0, cpu_buffer->cpu);
6204-
if (meta)
6205-
rb_meta_init_text_addr(meta);
6206-
62076168
atomic_dec(&cpu_buffer->record_disabled);
62086169
atomic_sub(RESET_BIT, &cpu_buffer->resize_disabled);
62096170
}

kernel/trace/trace.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5988,8 +5988,14 @@ ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
59885988
return __tracing_resize_ring_buffer(tr, size, cpu_id);
59895989
}
59905990

5991+
struct trace_scratch {
5992+
unsigned long kaslr_addr;
5993+
};
5994+
59915995
static void update_last_data(struct trace_array *tr)
59925996
{
5997+
struct trace_scratch *tscratch;
5998+
59935999
if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
59946000
return;
59956001

@@ -6004,6 +6010,17 @@ static void update_last_data(struct trace_array *tr)
60046010
/* Using current data now */
60056011
tr->text_delta = 0;
60066012

6013+
if (!tr->scratch)
6014+
return;
6015+
6016+
tscratch = tr->scratch;
6017+
6018+
/* Set the persistent ring buffer meta data to this address */
6019+
#ifdef CONFIG_RANDOMIZE_BASE
6020+
tscratch->kaslr_addr = kaslr_offset();
6021+
#else
6022+
tscratch->kaslr_addr = 0;
6023+
#endif
60076024
tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
60086025
}
60096026

@@ -6817,6 +6834,7 @@ static ssize_t
68176834
tracing_last_boot_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
68186835
{
68196836
struct trace_array *tr = filp->private_data;
6837+
struct trace_scratch *tscratch = tr->scratch;
68206838
struct seq_buf seq;
68216839
char buf[64];
68226840

@@ -6829,8 +6847,8 @@ tracing_last_boot_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t
68296847
* Otherwise it shows the KASLR address from the previous boot which
68306848
* should not be the same as the current boot.
68316849
*/
6832-
if (tr->flags & TRACE_ARRAY_FL_LAST_BOOT)
6833-
seq_buf_printf(&seq, "%lx\t[kernel]\n", tr->kaslr_addr);
6850+
if (tscratch && (tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
6851+
seq_buf_printf(&seq, "%lx\t[kernel]\n", tscratch->kaslr_addr);
68346852
else
68356853
seq_buf_puts(&seq, "# Current\n");
68366854

@@ -9210,6 +9228,8 @@ static int
92109228
allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
92119229
{
92129230
enum ring_buffer_flags rb_flags;
9231+
struct trace_scratch *tscratch;
9232+
unsigned int scratch_size;
92139233

92149234
rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
92159235

@@ -9218,12 +9238,19 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size
92189238
if (tr->range_addr_start && tr->range_addr_size) {
92199239
buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
92209240
tr->range_addr_start,
9221-
tr->range_addr_size, 0);
9241+
tr->range_addr_size,
9242+
sizeof(*tscratch));
9243+
9244+
tscratch = ring_buffer_meta_scratch(buf->buffer, &scratch_size);
9245+
if (tscratch) {
9246+
tr->scratch = tscratch;
9247+
tr->scratch_size = scratch_size;
92229248

92239249
#ifdef CONFIG_RANDOMIZE_BASE
9224-
if (ring_buffer_last_boot_delta(buf->buffer, &tr->kaslr_addr))
9225-
tr->text_delta = kaslr_offset() - tr->kaslr_addr;
9250+
if (tscratch->kaslr_addr)
9251+
tr->text_delta = kaslr_offset() - tscratch->kaslr_addr;
92269252
#endif
9253+
}
92279254
/*
92289255
* This is basically the same as a mapped buffer,
92299256
* with the same restrictions.

kernel/trace/trace.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,11 @@ struct trace_array {
348348
unsigned int mapped;
349349
unsigned long range_addr_start;
350350
unsigned long range_addr_size;
351-
unsigned long kaslr_addr;
352351
long text_delta;
352+
void *scratch; /* pointer in persistent memory */
353+
int scratch_size;
354+
355+
int buffer_disabled;
353356

354357
struct trace_pid_list __rcu *filtered_pids;
355358
struct trace_pid_list __rcu *filtered_no_pids;
@@ -367,7 +370,6 @@ struct trace_array {
367370
* CONFIG_TRACER_MAX_TRACE.
368371
*/
369372
arch_spinlock_t max_lock;
370-
int buffer_disabled;
371373
#ifdef CONFIG_FTRACE_SYSCALLS
372374
int sys_refcount_enter;
373375
int sys_refcount_exit;

0 commit comments

Comments
 (0)