ring-buffer: Force absolute timestamp on discard of event

commit b2dd797543 upstream.

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/20231206100244.5130f9b3@gandalf.local.home

Cc: stable@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fixes: 6f6be606e7 ("ring-buffer: Force before_stamp and write_stamp to be different on discard")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Steven Rostedt (Google) 2023-12-06 10:02:44 -05:00 committed by Greg Kroah-Hartman
parent d251b98183
commit 56a334310f

View File

@ -3030,22 +3030,19 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
local_read(&bpage->write) & ~RB_WRITE_MASK; local_read(&bpage->write) & ~RB_WRITE_MASK;
unsigned long event_length = rb_event_length(event); unsigned long event_length = rb_event_length(event);
/*
* For the before_stamp to be different than the write_stamp
* to make sure that the next event adds an absolute
* value and does not rely on the saved write stamp, which
* is now going to be bogus.
*/
rb_time_set(&cpu_buffer->before_stamp, 0);
/* Something came in, can't discard */ /* Something came in, can't discard */
if (!rb_time_cmpxchg(&cpu_buffer->write_stamp, if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
write_stamp, write_stamp - delta)) write_stamp, write_stamp - delta))
return false; return false;
/*
* It's possible that the event time delta is zero
* (has the same time stamp as the previous event)
* in which case write_stamp and before_stamp could
* be the same. In such a case, force before_stamp
* to be different than write_stamp. It doesn't
* matter what it is, as long as its different.
*/
if (!delta)
rb_time_set(&cpu_buffer->before_stamp, 0);
/* /*
* If an event were to come in now, it would see that the * If an event were to come in now, it would see that the
* write_stamp and the before_stamp are different, and assume * write_stamp and the before_stamp are different, and assume