Skip to content

Commit 10246fa

Browse files
committed
tracing: Use flag buffer_disabled for irqsoff tracer
If the ring buffer is disabled and the irqsoff tracer records a trace it will clear out its buffer and lose the data it had previously recorded. Currently there's a callback when writing to the tracing_of file, but if tracing is disabled via the function tracer trigger, it will not inform the irqsoff tracer to stop recording. By using the "mirror" flag (buffer_disabled) in the trace_array, that keeps track of the status of the trace_array's buffer, it gives the irqsoff tracer a fast way to know if it should record a new trace or not. The flag may be a little behind the real state of the buffer, but it should not affect the trace too much. It's more important for the irqsoff tracer to be fast. Reported-by: Dave Jones <davej@redhat.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent b04d52e commit 10246fa

2 files changed

Lines changed: 72 additions & 33 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,24 @@ cycle_t ftrace_now(int cpu)
226226
return ts;
227227
}
228228

229+
/**
230+
* tracing_is_enabled - Show if global_trace has been disabled
231+
*
232+
* Shows if the global trace has been enabled or not. It uses the
233+
* mirror flag "buffer_disabled" to be used in fast paths such as for
234+
* the irqsoff tracer. But it may be inaccurate due to races. If you
235+
* need to know the accurate state, use tracing_is_on() which is a little
236+
* slower, but accurate.
237+
*/
229238
int tracing_is_enabled(void)
230239
{
231-
return tracing_is_on();
240+
/*
241+
* For quick access (irqsoff uses this in fast path), just
242+
* return the mirror variable of the state of the ring buffer.
243+
* It's a little racy, but we don't really care.
244+
*/
245+
smp_rmb();
246+
return !global_trace.buffer_disabled;
232247
}
233248

234249
/*
@@ -341,6 +356,23 @@ unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
341356
TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
342357
TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
343358

359+
void tracer_tracing_on(struct trace_array *tr)
360+
{
361+
if (tr->trace_buffer.buffer)
362+
ring_buffer_record_on(tr->trace_buffer.buffer);
363+
/*
364+
* This flag is looked at when buffers haven't been allocated
365+
* yet, or by some tracers (like irqsoff), that just want to
366+
* know if the ring buffer has been disabled, but it can handle
367+
* races of where it gets disabled but we still do a record.
368+
* As the check is in the fast path of the tracers, it is more
369+
* important to be fast than accurate.
370+
*/
371+
tr->buffer_disabled = 0;
372+
/* Make the flag seen by readers */
373+
smp_wmb();
374+
}
375+
344376
/**
345377
* tracing_on - enable tracing buffers
346378
*
@@ -349,15 +381,7 @@ unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
349381
*/
350382
void tracing_on(void)
351383
{
352-
if (global_trace.trace_buffer.buffer)
353-
ring_buffer_record_on(global_trace.trace_buffer.buffer);
354-
/*
355-
* This flag is only looked at when buffers haven't been
356-
* allocated yet. We don't really care about the race
357-
* between setting this flag and actually turning
358-
* on the buffer.
359-
*/
360-
global_trace.buffer_disabled = 0;
384+
tracer_tracing_on(&global_trace);
361385
}
362386
EXPORT_SYMBOL_GPL(tracing_on);
363387

@@ -551,6 +575,23 @@ void tracing_snapshot_alloc(void)
551575
EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
552576
#endif /* CONFIG_TRACER_SNAPSHOT */
553577

578+
void tracer_tracing_off(struct trace_array *tr)
579+
{
580+
if (tr->trace_buffer.buffer)
581+
ring_buffer_record_off(tr->trace_buffer.buffer);
582+
/*
583+
* This flag is looked at when buffers haven't been allocated
584+
* yet, or by some tracers (like irqsoff), that just want to
585+
* know if the ring buffer has been disabled, but it can handle
586+
* races of where it gets disabled but we still do a record.
587+
* As the check is in the fast path of the tracers, it is more
588+
* important to be fast than accurate.
589+
*/
590+
tr->buffer_disabled = 1;
591+
/* Make the flag seen by readers */
592+
smp_wmb();
593+
}
594+
554595
/**
555596
* tracing_off - turn off tracing buffers
556597
*
@@ -561,15 +602,7 @@ EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
561602
*/
562603
void tracing_off(void)
563604
{
564-
if (global_trace.trace_buffer.buffer)
565-
ring_buffer_record_off(global_trace.trace_buffer.buffer);
566-
/*
567-
* This flag is only looked at when buffers haven't been
568-
* allocated yet. We don't really care about the race
569-
* between setting this flag and actually turning
570-
* on the buffer.
571-
*/
572-
global_trace.buffer_disabled = 1;
605+
tracer_tracing_off(&global_trace);
573606
}
574607
EXPORT_SYMBOL_GPL(tracing_off);
575608

@@ -579,14 +612,25 @@ void disable_trace_on_warning(void)
579612
tracing_off();
580613
}
581614

615+
/**
616+
* tracer_tracing_is_on - show real state of ring buffer enabled
617+
* @tr : the trace array to know if ring buffer is enabled
618+
*
619+
* Shows real state of the ring buffer if it is enabled or not.
620+
*/
621+
int tracer_tracing_is_on(struct trace_array *tr)
622+
{
623+
if (tr->trace_buffer.buffer)
624+
return ring_buffer_record_is_on(tr->trace_buffer.buffer);
625+
return !tr->buffer_disabled;
626+
}
627+
582628
/**
583629
* tracing_is_on - show state of ring buffers enabled
584630
*/
585631
int tracing_is_on(void)
586632
{
587-
if (global_trace.trace_buffer.buffer)
588-
return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
589-
return !global_trace.buffer_disabled;
633+
return tracer_tracing_is_on(&global_trace);
590634
}
591635
EXPORT_SYMBOL_GPL(tracing_is_on);
592636

@@ -3958,7 +4002,7 @@ static int tracing_wait_pipe(struct file *filp)
39584002
*
39594003
* iter->pos will be 0 if we haven't read anything.
39604004
*/
3961-
if (!tracing_is_enabled() && iter->pos)
4005+
if (!tracing_is_on() && iter->pos)
39624006
break;
39634007
}
39644008

@@ -5631,15 +5675,10 @@ rb_simple_read(struct file *filp, char __user *ubuf,
56315675
size_t cnt, loff_t *ppos)
56325676
{
56335677
struct trace_array *tr = filp->private_data;
5634-
struct ring_buffer *buffer = tr->trace_buffer.buffer;
56355678
char buf[64];
56365679
int r;
56375680

5638-
if (buffer)
5639-
r = ring_buffer_record_is_on(buffer);
5640-
else
5641-
r = 0;
5642-
5681+
r = tracer_tracing_is_on(tr);
56435682
r = sprintf(buf, "%d\n", r);
56445683

56455684
return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
@@ -5661,11 +5700,11 @@ rb_simple_write(struct file *filp, const char __user *ubuf,
56615700
if (buffer) {
56625701
mutex_lock(&trace_types_lock);
56635702
if (val) {
5664-
ring_buffer_record_on(buffer);
5703+
tracer_tracing_on(tr);
56655704
if (tr->current_trace->start)
56665705
tr->current_trace->start(tr);
56675706
} else {
5668-
ring_buffer_record_off(buffer);
5707+
tracer_tracing_off(tr);
56695708
if (tr->current_trace->stop)
56705709
tr->current_trace->stop(tr);
56715710
}

‎kernel/trace/trace_irqsoff.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ start_critical_timing(unsigned long ip, unsigned long parent_ip)
373373
struct trace_array_cpu *data;
374374
unsigned long flags;
375375

376-
if (likely(!tracer_enabled))
376+
if (!tracer_enabled || !tracing_is_enabled())
377377
return;
378378

379379
cpu = raw_smp_processor_id();
@@ -416,7 +416,7 @@ stop_critical_timing(unsigned long ip, unsigned long parent_ip)
416416
else
417417
return;
418418

419-
if (!tracer_enabled)
419+
if (!tracer_enabled || !tracing_is_enabled())
420420
return;
421421

422422
data = per_cpu_ptr(tr->trace_buffer.data, cpu);

0 commit comments

Comments
 (0)