Skip to contentSkip to conversation

ring-buffer: Remove useless update to write_stamp in rb_try_to_discard()

PATCH Superseded Steven Rostedt 1 participant 2 messages Dec 15, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH 1/1 ring-buffer: Remove useless update to write_stamp in rb_try_to_discard()

From: "Steven Rostedt (Google)" <[email protected]>

When filtering is enabled, a temporary buffer is created to place the content of the trace event output so that the filter logic can decide from the trace event output if the trace event should be filtered out or not. If it is to be filtered out, the content in the temporary buffer is simply discarded, otherwise it is written into the trace buffer.

But if an interrupt were to come in while a previous event was using that temporary buffer, the event written by the interrupt would actually go into the ring buffer itself to prevent corrupting the data on the temporary buffer. If the event is to be filtered out, the event in the ring buffer is discarded, or if it fails to discard because another event were to have already come in, it is turned into padding.

The update to the write_stamp in the rb_try_to_discard() happens after a fix was made to force the next event after the discard to use an absolute timestamp by setting the before_stamp to zero so it does not match the write_stamp (which causes an event to use the absolute timestamp).

But there's an effort in rb_try_to_discard() to put back the write_stamp to what it was before the event was added. But this is useless and wasteful because nothing is going to be using that write_stamp for calculations as it still will not match the before_stamp.

Remove this useless update, and in doing so, we remove another cmpxchg64()!

Also update the comments to reflect this change as well as remove some extra white space in another comment.

Fixes b2dd797543cf ("ring-buffer: Force absolute timestamp on discard of event")
---
 kernel/trace/ring_buffer.c | 46 +++++++++-----------------------------
 1 file changed, 11 insertions(+), 35 deletions(-)
kernel/trace/ring_buffer.c +11 -35
index 378ff9e53fe6..56b97a16ea16 100644
@@ -2792,25 +2792,6 @@ static unsigned rb_calculate_event_length(unsigned length)
 	return length;
 }
 
-static u64 rb_time_delta(struct ring_buffer_event *event)
-{
-	switch (event->type_len) {
-	case RINGBUF_TYPE_PADDING:
-		return 0;
-
-	case RINGBUF_TYPE_TIME_EXTEND:
-		return rb_event_time_stamp(event);
-
-	case RINGBUF_TYPE_TIME_STAMP:
-		return 0;
-
-	case RINGBUF_TYPE_DATA:
-		return event->time_delta;
-	default:
-		return 0;
-	}
-}
-
 static inline bool
 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
 		  struct ring_buffer_event *event)
@@ -2818,8 +2799,6 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
 	unsigned long new_index, old_index;
 	struct buffer_page *bpage;
 	unsigned long addr;
-	u64 write_stamp;
-	u64 delta;
 
 	new_index = rb_event_index(event);
 	old_index = new_index + rb_event_ts_length(event);
@@ -2828,13 +2807,10 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
 
 	bpage = READ_ONCE(cpu_buffer->tail_page);
 
-	delta = rb_time_delta(event);
-
-	rb_time_read(&cpu_buffer->write_stamp, &write_stamp);
-
-	/* Make sure the write stamp is read before testing the location */
-	barrier();
-
+	/*
+	 * Make sure the tail_page is still the same and
+	 * the next write location is the end of this event
+	 */
 	if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
 		unsigned long write_mask =
 			local_read(&bpage->write) & ~RB_WRITE_MASK;
@@ -2845,20 +2821,20 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
 		 * 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.
+		 *
+		 * By setting the before_stamp to zero, the next event
+		 * is not going to use the write_stamp and will instead
+		 * create an absolute timestamp. This means there's no
+		 * reason to update the wirte_stamp!
 		 */
 		rb_time_set(&cpu_buffer->before_stamp, 0);
 
-		/* Something came in, can't discard */
-		if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
-				       write_stamp, write_stamp - delta))
-			return false;
-
 		/*
 		 * If an event were to come in now, it would see that the
 		 * write_stamp and the before_stamp are different, and assume
 		 * that this event just added itself before updating
 		 * the write stamp. The interrupting event will fix the
-		 * write stamp for us, and use the before stamp as its delta.
+		 * write stamp for us, and use an absolute timestamp.
 		 */
 
 		/*
@@ -3295,7 +3271,7 @@ static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
 		return;
 
 	/*
-	 * If this interrupted another event, 
+	 * If this interrupted another event,
 	 */
 	if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
 		goto out;
Signature · 1 line
2.42.0
author maintainer
#1 lore raw
Quoted 20 lines · On Fri, 15 Dec 2023 07:41:51 -0500 Steven Rostedt <[email protected]> wrote:

From: "Steven Rostedt (Google)" <[email protected]>

When filtering is enabled, a temporary buffer is created to place the content of the trace event output so that the filter logic can decide from the trace event output if the trace event should be filtered out or not. If it is to be filtered out, the content in the temporary buffer is simply discarded, otherwise it is written into the trace buffer.

But if an interrupt were to come in while a previous event was using that temporary buffer, the event written by the interrupt would actually go into the ring buffer itself to prevent corrupting the data on the temporary buffer. If the event is to be filtered out, the event in the ring buffer is discarded, or if it fails to discard because another event were to have already come in, it is turned into padding.

The update to the write_stamp in the rb_try_to_discard() happens after a fix was made to force the next event after the discard to use an absolute timestamp by setting the before_stamp to zero so it does not match the write_stamp (which causes an event to use the absolute timestamp).

Quoted 7 lines

But there's an effort in rb_try_to_discard() to put back the write_stamp to what it was before the event was added. But this is useless and wasteful because nothing is going to be using that write_stamp for calculations as it still will not match the before_stamp.

Remove this useless update, and in doing so, we remove another cmpxchg64()!

While debugging the timestamp issues, I found that the cmpxchg64 in the discard was actually useless after commit b2dd797543cf (in the fixes below). This removes the third of the 4 cmpxchg64 in the ring buffer code. The last one has:

		u64 ts;
		/* SLOW PATH - Interrupted between A and C */
		rb_time_read(&cpu_buffer->write_stamp, &info->after);
		ts = rb_time_stamp(cpu_buffer->buffer);
		barrier();
 /*E*/		if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
		    info->after < ts &&
		    rb_time_cmpxchg(&cpu_buffer->write_stamp,
				    info->after, ts)) {
			/* Nothing came after this event between C and E */
			info->delta = ts - info->after;
		} else {

That could actually be changed to:

/*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&

		    info->after < ts && info->after == READ_ONCE(cpu_buffer->write_stamp)){
			/* Nothing came after this event between C and E */
			info->delta = ts - info->after;

where the only difference is that the before_stamp and write_stamp will not match making the next event add an absolute_timestamp.

This would get rid of all the cmpxchg64(). I could have:

/*
* Returns true if ts == old_ts, and if possible will update with new_ts,
* but ts is not guaranteed to be updated even if this returns true */
static bool rb_time_cmp_and_update(rb_time_t *ts, u64 old_ts, u64 new_ts) {

#if HAVE_CMPXCHG64
	return local64_cmpxchg(ts, old_ts, new_ts) == old_ts;
#else
	return old_ts == READ_ONCE(*ts);

#endif
}

/*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&

		    info->after < ts &&
		    rb_time_cmp_and_update(&cpu_buffer->write_stamp, info->after, ts) {
			/* Nothing came after this event between C and E */
			info->delta = ts - info->after;

And this way we don't even need any CMPXCHG64. And for those that do have it, it gets the benefit of not having to insert an absolute timestamp for the next event. That's just a nice-to-have and not critical for the logic.

-- Steve

Quoted 7 lines

Also update the comments to reflect this change as well as remove some extra white space in another comment.

Fixes b2dd797543cf ("ring-buffer: Force absolute timestamp on discard of event")

---