Skip to content

Commit a7603ff

Browse files
Steven Rostedtrostedt
authored andcommitted
tracing: Replace the static global per_cpu arrays with allocated per_cpu
The global and max-tr currently use static per_cpu arrays for the CPU data descriptors. But in order to get new allocated trace_arrays, they need to be allocated per_cpu arrays. Instead of using the static arrays, switch the global and max-tr to use allocated data. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent ccb469a commit a7603ff

10 files changed

Lines changed: 79 additions & 61 deletions

‎kernel/trace/trace.c‎

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ static struct trace_array global_trace;
191191

192192
LIST_HEAD(ftrace_trace_arrays);
193193

194-
static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
195-
196194
int filter_current_check_discard(struct ring_buffer *buffer,
197195
struct ftrace_event_call *call, void *rec,
198196
struct ring_buffer_event *event)
@@ -227,8 +225,6 @@ cycle_t ftrace_now(int cpu)
227225
*/
228226
static struct trace_array max_tr;
229227

230-
static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
231-
232228
int tracing_is_enabled(void)
233229
{
234230
return tracing_is_on();
@@ -666,13 +662,13 @@ unsigned long __read_mostly tracing_max_latency;
666662
static void
667663
__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
668664
{
669-
struct trace_array_cpu *data = tr->data[cpu];
665+
struct trace_array_cpu *data = per_cpu_ptr(tr->data, cpu);
670666
struct trace_array_cpu *max_data;
671667

672668
max_tr.cpu = cpu;
673669
max_tr.time_start = data->preempt_timestamp;
674670

675-
max_data = max_tr.data[cpu];
671+
max_data = per_cpu_ptr(max_tr.data, cpu);
676672
max_data->saved_latency = tracing_max_latency;
677673
max_data->critical_start = data->critical_start;
678674
max_data->critical_end = data->critical_end;
@@ -1984,7 +1980,7 @@ void tracing_iter_reset(struct trace_iterator *iter, int cpu)
19841980
unsigned long entries = 0;
19851981
u64 ts;
19861982

1987-
tr->data[cpu]->skipped_entries = 0;
1983+
per_cpu_ptr(tr->data, cpu)->skipped_entries = 0;
19881984

19891985
buf_iter = trace_buffer_iter(iter, cpu);
19901986
if (!buf_iter)
@@ -2004,7 +2000,7 @@ void tracing_iter_reset(struct trace_iterator *iter, int cpu)
20042000
ring_buffer_read(buf_iter, NULL);
20052001
}
20062002

2007-
tr->data[cpu]->skipped_entries = entries;
2003+
per_cpu_ptr(tr->data, cpu)->skipped_entries = entries;
20082004
}
20092005

20102006
/*
@@ -2099,8 +2095,8 @@ get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *e
20992095
* entries for the trace and we need to ignore the
21002096
* ones before the time stamp.
21012097
*/
2102-
if (tr->data[cpu]->skipped_entries) {
2103-
count -= tr->data[cpu]->skipped_entries;
2098+
if (per_cpu_ptr(tr->data, cpu)->skipped_entries) {
2099+
count -= per_cpu_ptr(tr->data, cpu)->skipped_entries;
21042100
/* total is the same as the entries */
21052101
*total += count;
21062102
} else
@@ -2157,7 +2153,7 @@ print_trace_header(struct seq_file *m, struct trace_iterator *iter)
21572153
{
21582154
unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
21592155
struct trace_array *tr = iter->tr;
2160-
struct trace_array_cpu *data = tr->data[tr->cpu];
2156+
struct trace_array_cpu *data = per_cpu_ptr(tr->data, tr->cpu);
21612157
struct tracer *type = iter->trace;
21622158
unsigned long entries;
21632159
unsigned long total;
@@ -2227,7 +2223,7 @@ static void test_cpu_buff_start(struct trace_iterator *iter)
22272223
if (cpumask_test_cpu(iter->cpu, iter->started))
22282224
return;
22292225

2230-
if (iter->tr->data[iter->cpu]->skipped_entries)
2226+
if (per_cpu_ptr(iter->tr->data, iter->cpu)->skipped_entries)
22312227
return;
22322228

22332229
cpumask_set_cpu(iter->cpu, iter->started);
@@ -2858,12 +2854,12 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
28582854
*/
28592855
if (cpumask_test_cpu(cpu, tracing_cpumask) &&
28602856
!cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2861-
atomic_inc(&tr->data[cpu]->disabled);
2857+
atomic_inc(&per_cpu_ptr(tr->data, cpu)->disabled);
28622858
ring_buffer_record_disable_cpu(tr->buffer, cpu);
28632859
}
28642860
if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
28652861
cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2866-
atomic_dec(&tr->data[cpu]->disabled);
2862+
atomic_dec(&per_cpu_ptr(tr->data, cpu)->disabled);
28672863
ring_buffer_record_enable_cpu(tr->buffer, cpu);
28682864
}
28692865
}
@@ -3177,7 +3173,7 @@ static void set_buffer_entries(struct trace_array *tr, unsigned long val)
31773173
{
31783174
int cpu;
31793175
for_each_tracing_cpu(cpu)
3180-
tr->data[cpu]->entries = val;
3176+
per_cpu_ptr(tr->data, cpu)->entries = val;
31813177
}
31823178

31833179
/* resize @tr's buffer to the size of @size_tr's entries */
@@ -3189,17 +3185,18 @@ static int resize_buffer_duplicate_size(struct trace_array *tr,
31893185
if (cpu_id == RING_BUFFER_ALL_CPUS) {
31903186
for_each_tracing_cpu(cpu) {
31913187
ret = ring_buffer_resize(tr->buffer,
3192-
size_tr->data[cpu]->entries, cpu);
3188+
per_cpu_ptr(size_tr->data, cpu)->entries, cpu);
31933189
if (ret < 0)
31943190
break;
3195-
tr->data[cpu]->entries = size_tr->data[cpu]->entries;
3191+
per_cpu_ptr(tr->data, cpu)->entries =
3192+
per_cpu_ptr(size_tr->data, cpu)->entries;
31963193
}
31973194
} else {
31983195
ret = ring_buffer_resize(tr->buffer,
3199-
size_tr->data[cpu_id]->entries, cpu_id);
3196+
per_cpu_ptr(size_tr->data, cpu_id)->entries, cpu_id);
32003197
if (ret == 0)
3201-
tr->data[cpu_id]->entries =
3202-
size_tr->data[cpu_id]->entries;
3198+
per_cpu_ptr(tr->data, cpu_id)->entries =
3199+
per_cpu_ptr(size_tr->data, cpu_id)->entries;
32033200
}
32043201

32053202
return ret;
@@ -3256,13 +3253,13 @@ static int __tracing_resize_ring_buffer(struct trace_array *tr,
32563253
if (cpu == RING_BUFFER_ALL_CPUS)
32573254
set_buffer_entries(&max_tr, size);
32583255
else
3259-
max_tr.data[cpu]->entries = size;
3256+
per_cpu_ptr(max_tr.data, cpu)->entries = size;
32603257

32613258
out:
32623259
if (cpu == RING_BUFFER_ALL_CPUS)
32633260
set_buffer_entries(tr, size);
32643261
else
3265-
tr->data[cpu]->entries = size;
3262+
per_cpu_ptr(tr->data, cpu)->entries = size;
32663263

32673264
return ret;
32683265
}
@@ -3905,8 +3902,8 @@ tracing_entries_read(struct file *filp, char __user *ubuf,
39053902
for_each_tracing_cpu(cpu) {
39063903
/* fill in the size from first enabled cpu */
39073904
if (size == 0)
3908-
size = tr->data[cpu]->entries;
3909-
if (size != tr->data[cpu]->entries) {
3905+
size = per_cpu_ptr(tr->data, cpu)->entries;
3906+
if (size != per_cpu_ptr(tr->data, cpu)->entries) {
39103907
buf_size_same = 0;
39113908
break;
39123909
}
@@ -3922,7 +3919,7 @@ tracing_entries_read(struct file *filp, char __user *ubuf,
39223919
} else
39233920
r = sprintf(buf, "X\n");
39243921
} else
3925-
r = sprintf(buf, "%lu\n", tr->data[tc->cpu]->entries >> 10);
3922+
r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->data, tc->cpu)->entries >> 10);
39263923

39273924
mutex_unlock(&trace_types_lock);
39283925

@@ -3969,7 +3966,7 @@ tracing_total_entries_read(struct file *filp, char __user *ubuf,
39693966

39703967
mutex_lock(&trace_types_lock);
39713968
for_each_tracing_cpu(cpu) {
3972-
size += tr->data[cpu]->entries >> 10;
3969+
size += per_cpu_ptr(tr->data, cpu)->entries >> 10;
39733970
if (!ring_buffer_expanded)
39743971
expanded_size += trace_buf_size >> 10;
39753972
}
@@ -4773,7 +4770,7 @@ static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
47734770
static void
47744771
tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
47754772
{
4776-
struct trace_array_cpu *data = tr->data[cpu];
4773+
struct trace_array_cpu *data = per_cpu_ptr(tr->data, cpu);
47774774
struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
47784775
struct dentry *d_cpu;
47794776
char cpu_dir[30]; /* 30 characters should be more than enough */
@@ -5298,7 +5295,7 @@ __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
52985295
trace_init_global_iter(&iter);
52995296

53005297
for_each_tracing_cpu(cpu) {
5301-
atomic_inc(&iter.tr->data[cpu]->disabled);
5298+
atomic_inc(&per_cpu_ptr(iter.tr->data, cpu)->disabled);
53025299
}
53035300

53045301
old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
@@ -5366,7 +5363,7 @@ __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
53665363
trace_flags |= old_userobj;
53675364

53685365
for_each_tracing_cpu(cpu) {
5369-
atomic_dec(&iter.tr->data[cpu]->disabled);
5366+
atomic_dec(&per_cpu_ptr(iter.tr->data, cpu)->disabled);
53705367
}
53715368
tracing_on();
53725369
}
@@ -5422,11 +5419,31 @@ __init static int tracer_alloc_buffers(void)
54225419
WARN_ON(1);
54235420
goto out_free_cpumask;
54245421
}
5422+
5423+
global_trace.data = alloc_percpu(struct trace_array_cpu);
5424+
5425+
if (!global_trace.data) {
5426+
printk(KERN_ERR "tracer: failed to allocate percpu memory!\n");
5427+
WARN_ON(1);
5428+
goto out_free_cpumask;
5429+
}
5430+
5431+
for_each_tracing_cpu(i) {
5432+
memset(per_cpu_ptr(global_trace.data, i), 0, sizeof(struct trace_array_cpu));
5433+
per_cpu_ptr(global_trace.data, i)->trace_cpu.cpu = i;
5434+
per_cpu_ptr(global_trace.data, i)->trace_cpu.tr = &global_trace;
5435+
}
5436+
54255437
if (global_trace.buffer_disabled)
54265438
tracing_off();
54275439

5428-
54295440
#ifdef CONFIG_TRACER_MAX_TRACE
5441+
max_tr.data = alloc_percpu(struct trace_array_cpu);
5442+
if (!max_tr.data) {
5443+
printk(KERN_ERR "tracer: failed to allocate percpu memory!\n");
5444+
WARN_ON(1);
5445+
goto out_free_cpumask;
5446+
}
54305447
max_tr.buffer = ring_buffer_alloc(1, rb_flags);
54315448
raw_spin_lock_init(&max_tr.start_lock);
54325449
if (!max_tr.buffer) {
@@ -5435,18 +5452,15 @@ __init static int tracer_alloc_buffers(void)
54355452
ring_buffer_free(global_trace.buffer);
54365453
goto out_free_cpumask;
54375454
}
5438-
#endif
54395455

5440-
/* Allocate the first page for all buffers */
54415456
for_each_tracing_cpu(i) {
5442-
global_trace.data[i] = &per_cpu(global_trace_cpu, i);
5443-
global_trace.data[i]->trace_cpu.cpu = i;
5444-
global_trace.data[i]->trace_cpu.tr = &global_trace;
5445-
max_tr.data[i] = &per_cpu(max_tr_data, i);
5446-
max_tr.data[i]->trace_cpu.cpu = i;
5447-
max_tr.data[i]->trace_cpu.tr = &max_tr;
5457+
memset(per_cpu_ptr(max_tr.data, i), 0, sizeof(struct trace_array_cpu));
5458+
per_cpu_ptr(max_tr.data, i)->trace_cpu.cpu = i;
5459+
per_cpu_ptr(max_tr.data, i)->trace_cpu.tr = &max_tr;
54485460
}
5461+
#endif
54495462

5463+
/* Allocate the first page for all buffers */
54505464
set_buffer_entries(&global_trace,
54515465
ring_buffer_size(global_trace.buffer, 0));
54525466
#ifdef CONFIG_TRACER_MAX_TRACE
@@ -5488,6 +5502,8 @@ __init static int tracer_alloc_buffers(void)
54885502
return 0;
54895503

54905504
out_free_cpumask:
5505+
free_percpu(global_trace.data);
5506+
free_percpu(max_tr.data);
54915507
free_cpumask_var(tracing_cpumask);
54925508
out_free_buffer_mask:
54935509
free_cpumask_var(tracing_buffer_mask);

‎kernel/trace/trace.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ struct trace_array {
186186
struct list_head systems;
187187
struct list_head events;
188188
struct task_struct *waiter;
189-
struct trace_array_cpu *data[NR_CPUS];
189+
struct trace_array_cpu *data;
190190
};
191191

192192
enum {

‎kernel/trace/trace_branch.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
3232
{
3333
struct ftrace_event_call *call = &event_branch;
3434
struct trace_array *tr = branch_tracer;
35+
struct trace_array_cpu *data;
3536
struct ring_buffer_event *event;
3637
struct trace_branch *entry;
3738
struct ring_buffer *buffer;
@@ -51,7 +52,8 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
5152

5253
local_irq_save(flags);
5354
cpu = raw_smp_processor_id();
54-
if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
55+
data = per_cpu_ptr(tr->data, cpu);
56+
if (atomic_inc_return(&data->disabled) != 1)
5557
goto out;
5658

5759
pc = preempt_count();
@@ -80,7 +82,7 @@ probe_likely_condition(struct ftrace_branch_data *f, int val, int expect)
8082
__buffer_unlock_commit(buffer, event);
8183

8284
out:
83-
atomic_dec(&tr->data[cpu]->disabled);
85+
atomic_dec(&data->disabled);
8486
local_irq_restore(flags);
8587
}
8688

‎kernel/trace/trace_functions.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function_trace_call(unsigned long ip, unsigned long parent_ip,
7676
goto out;
7777

7878
cpu = smp_processor_id();
79-
data = tr->data[cpu];
79+
data = per_cpu_ptr(tr->data, cpu);
8080
if (!atomic_read(&data->disabled)) {
8181
local_save_flags(flags);
8282
trace_function(tr, ip, parent_ip, flags, pc);
@@ -107,7 +107,7 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
107107
*/
108108
local_irq_save(flags);
109109
cpu = raw_smp_processor_id();
110-
data = tr->data[cpu];
110+
data = per_cpu_ptr(tr->data, cpu);
111111
disabled = atomic_inc_return(&data->disabled);
112112

113113
if (likely(disabled == 1)) {

‎kernel/trace/trace_functions_graph.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ int trace_graph_entry(struct ftrace_graph_ent *trace)
265265

266266
local_irq_save(flags);
267267
cpu = raw_smp_processor_id();
268-
data = tr->data[cpu];
268+
data = per_cpu_ptr(tr->data, cpu);
269269
disabled = atomic_inc_return(&data->disabled);
270270
if (likely(disabled == 1)) {
271271
pc = preempt_count();
@@ -350,7 +350,7 @@ void trace_graph_return(struct ftrace_graph_ret *trace)
350350

351351
local_irq_save(flags);
352352
cpu = raw_smp_processor_id();
353-
data = tr->data[cpu];
353+
data = per_cpu_ptr(tr->data, cpu);
354354
disabled = atomic_inc_return(&data->disabled);
355355
if (likely(disabled == 1)) {
356356
pc = preempt_count();

‎kernel/trace/trace_irqsoff.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static int func_prolog_dec(struct trace_array *tr,
121121
if (!irqs_disabled_flags(*flags))
122122
return 0;
123123

124-
*data = tr->data[cpu];
124+
*data = per_cpu_ptr(tr->data, cpu);
125125
disabled = atomic_inc_return(&(*data)->disabled);
126126

127127
if (likely(disabled == 1))
@@ -380,7 +380,7 @@ start_critical_timing(unsigned long ip, unsigned long parent_ip)
380380
if (per_cpu(tracing_cpu, cpu))
381381
return;
382382

383-
data = tr->data[cpu];
383+
data = per_cpu_ptr(tr->data, cpu);
384384

385385
if (unlikely(!data) || atomic_read(&data->disabled))
386386
return;
@@ -418,7 +418,7 @@ stop_critical_timing(unsigned long ip, unsigned long parent_ip)
418418
if (!tracer_enabled)
419419
return;
420420

421-
data = tr->data[cpu];
421+
data = per_cpu_ptr(tr->data, cpu);
422422

423423
if (unlikely(!data) ||
424424
!data->critical_start || atomic_read(&data->disabled))

‎kernel/trace/trace_kdb.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void ftrace_dump_buf(int skip_lines, long cpu_file)
2626
trace_init_global_iter(&iter);
2727

2828
for_each_tracing_cpu(cpu) {
29-
atomic_inc(&iter.tr->data[cpu]->disabled);
29+
atomic_inc(&per_cpu_ptr(iter.tr->data, cpu)->disabled);
3030
}
3131

3232
old_userobj = trace_flags;
@@ -83,7 +83,7 @@ static void ftrace_dump_buf(int skip_lines, long cpu_file)
8383
trace_flags = old_userobj;
8484

8585
for_each_tracing_cpu(cpu) {
86-
atomic_dec(&iter.tr->data[cpu]->disabled);
86+
atomic_dec(&per_cpu_ptr(iter.tr->data, cpu)->disabled);
8787
}
8888

8989
for_each_tracing_cpu(cpu)

0 commit comments

Comments
 (0)