Skip to content

Commit b2dd797

Browse files
committed
ring-buffer: Force absolute timestamp on discard of event
There's a race where if an event is discarded from the ring buffer and an interrupt were to happen at that time and insert an event, the time stamp is still used from the discarded event as an offset. This can screw up the timings. If the event is going to be discarded, set the "before_stamp" to zero. When a new event comes in, it compares the "before_stamp" with the "write_stamp" and if they are not equal, it will insert an absolute timestamp. This will prevent the timings from getting out of sync due to the discarded event. Link: https://lore.kernel.org/linux-trace-kernel/[email protected] Cc: [email protected] Cc: Masami Hiramatsu <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Fixes: 6f6be60 ("ring-buffer: Force before_stamp and write_stamp to be different on discard") Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent c0591b1 commit b2dd797

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

kernel/trace/ring_buffer.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,22 +3030,19 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
30303030
local_read(&bpage->write) & ~RB_WRITE_MASK;
30313031
unsigned long event_length = rb_event_length(event);
30323032

3033+
/*
3034+
* For the before_stamp to be different than the write_stamp
3035+
* to make sure that the next event adds an absolute
3036+
* value and does not rely on the saved write stamp, which
3037+
* is now going to be bogus.
3038+
*/
3039+
rb_time_set(&cpu_buffer->before_stamp, 0);
3040+
30333041
/* Something came in, can't discard */
30343042
if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
30353043
write_stamp, write_stamp - delta))
30363044
return false;
30373045

3038-
/*
3039-
* It's possible that the event time delta is zero
3040-
* (has the same time stamp as the previous event)
3041-
* in which case write_stamp and before_stamp could
3042-
* be the same. In such a case, force before_stamp
3043-
* to be different than write_stamp. It doesn't
3044-
* matter what it is, as long as its different.
3045-
*/
3046-
if (!delta)
3047-
rb_time_set(&cpu_buffer->before_stamp, 0);
3048-
30493046
/*
30503047
* If an event were to come in now, it would see that the
30513048
* write_stamp and the before_stamp are different, and assume

0 commit comments

Comments
 (0)