Skip to content

Commit 2fd821e

Browse files
rostedtgregkh
authored andcommitted
tracing: Use flag buffer_disabled for irqsoff tracer
commit 10246fa upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e25d458 commit 2fd821e

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
@@ -246,9 +246,24 @@ cycle_t ftrace_now(int cpu)
246246
return ts;
247247
}
248248

249+
/**
250+
* tracing_is_enabled - Show if global_trace has been disabled
251+
*
252+
* Shows if the global trace has been enabled or not. It uses the
253+
* mirror flag "buffer_disabled" to be used in fast paths such as for
254+
* the irqsoff tracer. But it may be inaccurate due to races. If you
255+
* need to know the accurate state, use tracing_is_on() which is a little
256+
* slower, but accurate.
257+
*/
249258
int tracing_is_enabled(void)
250259
{
251-
return tracing_is_on();
260+
/*
261+
* For quick access (irqsoff uses this in fast path), just
262+
* return the mirror variable of the state of the ring buffer.
263+
* It's a little racy, but we don't really care.
264+
*/
265+
smp_rmb();
266+
return !global_trace.buffer_disabled;
252267
}
253268

254269
/*
@@ -361,6 +376,23 @@ unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
361376
TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
362377
TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
363378

379+
void tracer_tracing_on(struct trace_array *tr)
380+
{
381+
if (tr->trace_buffer.buffer)
382+
ring_buffer_record_on(tr->trace_buffer.buffer);
383+
/*
384+
* This flag is looked at when buffers haven't been allocated
385+
* yet, or by some tracers (like irqsoff), that just want to
386+
* know if the ring buffer has been disabled, but it can handle
387+
* races of where it gets disabled but we still do a record.
388+
* As the check is in the fast path of the tracers, it is more
389+
* important to be fast than accurate.
390+
*/
391+
tr->buffer_disabled = 0;
392+
/* Make the flag seen by readers */
393+
smp_wmb();
394+
}
395+
364396
/**
365397
* tracing_on - enable tracing buffers
366398
*
@@ -369,15 +401,7 @@ unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
369401
*/
370402
void tracing_on(void)
371403
{
372-
if (global_trace.trace_buffer.buffer)
373-
ring_buffer_record_on(global_trace.trace_buffer.buffer);
374-
/*
375-
* This flag is only looked at when buffers haven't been
376-
* allocated yet. We don't really care about the race
377-
* between setting this flag and actually turning
378-
* on the buffer.
379-
*/
380-
global_trace.buffer_disabled = 0;
404+
tracer_tracing_on(&global_trace);
381405
}
382406
EXPORT_SYMBOL_GPL(tracing_on);
383407

@@ -571,6 +595,23 @@ void tracing_snapshot_alloc(void)
571595
EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
572596
#endif /* CONFIG_TRACER_SNAPSHOT */
573597

598+
void tracer_tracing_off(struct trace_array *tr)
599+
{
600+
if (tr->trace_buffer.buffer)
601+
ring_buffer_record_off(tr->trace_buffer.buffer);
602+
/*
603+
* This flag is looked at when buffers haven't been allocated
604+
* yet, or by some tracers (like irqsoff), that just want to
605+
* know if the ring buffer has been disabled, but it can handle
606+
* races of where it gets disabled but we still do a record.
607+
* As the check is in the fast path of the tracers, it is more
608+
* important to be fast than accurate.
609+
*/
610+
tr->buffer_disabled = 1;
611+
/* Make the flag seen by readers */
612+
smp_wmb();
613+
}
614+
574615
/**
575616
* tracing_off - turn off tracing buffers
576617
*
@@ -581,26 +622,29 @@ EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
581622
*/
582623
void tracing_off(void)
583624
{
584-
if (global_trace.trace_buffer.buffer)
585-
ring_buffer_record_off(global_trace.trace_buffer.buffer);
586-
/*
587-
* This flag is only looked at when buffers haven't been
588-
* allocated yet. We don't really care about the race
589-
* between setting this flag and actually turning
590-
* on the buffer.
591-
*/
592-
global_trace.buffer_disabled = 1;
625+
tracer_tracing_off(&global_trace);
593626
}
594627
EXPORT_SYMBOL_GPL(tracing_off);
595628

629+
/**
630+
* tracer_tracing_is_on - show real state of ring buffer enabled
631+
* @tr : the trace array to know if ring buffer is enabled
632+
*
633+
* Shows real state of the ring buffer if it is enabled or not.
634+
*/
635+
int tracer_tracing_is_on(struct trace_array *tr)
636+
{
637+
if (tr->trace_buffer.buffer)
638+
return ring_buffer_record_is_on(tr->trace_buffer.buffer);
639+
return !tr->buffer_disabled;
640+
}
641+
596642
/**
597643
* tracing_is_on - show state of ring buffers enabled
598644
*/
599645
int tracing_is_on(void)
600646
{
601-
if (global_trace.trace_buffer.buffer)
602-
return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
603-
return !global_trace.buffer_disabled;
647+
return tracer_tracing_is_on(&global_trace);
604648
}
605649
EXPORT_SYMBOL_GPL(tracing_is_on);
606650

@@ -4060,7 +4104,7 @@ static int tracing_wait_pipe(struct file *filp)
40604104
*
40614105
* iter->pos will be 0 if we haven't read anything.
40624106
*/
4063-
if (!tracing_is_enabled() && iter->pos)
4107+
if (!tracing_is_on() && iter->pos)
40644108
break;
40654109
}
40664110

@@ -5772,15 +5816,10 @@ rb_simple_read(struct file *filp, char __user *ubuf,
57725816
size_t cnt, loff_t *ppos)
57735817
{
57745818
struct trace_array *tr = filp->private_data;
5775-
struct ring_buffer *buffer = tr->trace_buffer.buffer;
57765819
char buf[64];
57775820
int r;
57785821

5779-
if (buffer)
5780-
r = ring_buffer_record_is_on(buffer);
5781-
else
5782-
r = 0;
5783-
5822+
r = tracer_tracing_is_on(tr);
57845823
r = sprintf(buf, "%d\n", r);
57855824

57865825
return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
@@ -5802,11 +5841,11 @@ rb_simple_write(struct file *filp, const char __user *ubuf,
58025841
if (buffer) {
58035842
mutex_lock(&trace_types_lock);
58045843
if (val) {
5805-
ring_buffer_record_on(buffer);
5844+
tracer_tracing_on(tr);
58065845
if (tr->current_trace->start)
58075846
tr->current_trace->start(tr);
58085847
} else {
5809-
ring_buffer_record_off(buffer);
5848+
tracer_tracing_off(tr);
58105849
if (tr->current_trace->stop)
58115850
tr->current_trace->stop(tr);
58125851
}

‎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)