Skip to contentSkip to conversation

tracing: Clean up of set_tracer_flag()

PATCH In review Steven Rostedt 3 participants 6 messages Nov 6, 2025 – Nov 10, 2025 Tracing

lore
Share & tools
lore
#0 lore raw

cover tracing: Clean up of set_tracer_flag()

The set_tracer_flag() function takes a mask that has a single bit set. It then goes through a bunch of "if ()" statements looking at what the mask is to perform the necessary operations. It is simpler to switch that over to a "switch ()" statement.

Steven Rostedt (2):
tracing: Exit out immediately after update_marker_trace()
tracing: Use switch statement instead of ifs in set_tracer_flag()

----

 kernel/trace/trace.c | 41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)
#1 lore raw

PATCH 1/2 tracing: Exit out immediately after update_marker_trace()

From: Steven Rostedt <[email protected]>

The call to update_marker_trace() in set_tracer_flag() performs the update to the tr->trace_flags. There's no reason to perform it again after it is called. Return immediately instead.

---
 kernel/trace/trace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/trace/trace.c +4 -1
index dea1566b3301..07bd10808277 100644
@@ -5285,8 +5285,11 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 		}
 	}
 
-	if (mask == TRACE_ITER(COPY_MARKER))
+	if (mask == TRACE_ITER(COPY_MARKER)) {
 		update_marker_trace(tr, enabled);
+		/* update_marker_trace updates the tr->trace_flags */
+		return 0;
+	}
 
 	if (enabled)
 		tr->trace_flags |= mask;
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] tracing: Exit out immediately after update_marker_trace()

Quoted 6 lines · On Wed, 05 Nov 2025 19:33:25 -0500 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

The call to update_marker_trace() in set_tracer_flag() performs the update to the tr->trace_flags. There's no reason to perform it again after it is called. Return immediately instead.

Looks good to me.

Thanks,

Quoted 26 lines
---
 kernel/trace/trace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/trace/trace.c +4 -1
index dea1566b3301..07bd10808277 100644
@@ -5285,8 +5285,11 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 		}
 	}
 
-	if (mask == TRACE_ITER(COPY_MARKER))
+	if (mask == TRACE_ITER(COPY_MARKER)) {
 		update_marker_trace(tr, enabled);
+		/* update_marker_trace updates the tr->trace_flags */
+		return 0;
+	}
 
 	if (enabled)
 		tr->trace_flags |= mask;
Signature · 1 line
2.51.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#3 lore raw

PATCH 2/2 tracing: Use switch statement instead of ifs in set_tracer_flag()

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

The "mask" passed in to set_trace_flag() has a single bit set. The function then checks if the mask is equal to one of the option masks and performs the appropriate function associated to that option.

Instead of having a bunch of "if ()" statement, use a "switch ()" statement instead to make it cleaner and a bit more optimal.

No function changes.

---
 kernel/trace/trace.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)
kernel/trace/trace.c +23 -15
index 07bd10808277..8460bec9f263 100644
@@ -5251,11 +5251,13 @@ int trace_keep_overwrite(struct tracer *tracer, u64 mask, int set)
 
 int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 {
-	if ((mask == TRACE_ITER(RECORD_TGID)) ||
-	    (mask == TRACE_ITER(RECORD_CMD)) ||
-	    (mask == TRACE_ITER(TRACE_PRINTK)) ||
-	    (mask == TRACE_ITER(COPY_MARKER)))
+	switch (mask) {
+	case TRACE_ITER(RECORD_TGID):
+	case TRACE_ITER(RECORD_CMD):
+	case TRACE_ITER(TRACE_PRINTK):
+	case TRACE_ITER(COPY_MARKER):
 		lockdep_assert_held(&event_mutex);
+	}
 
 	/* do nothing if flag is already set */
 	if (!!(tr->trace_flags & mask) == !!enabled)
@@ -5266,7 +5268,8 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 		if (tr->current_trace->flag_changed(tr, mask, !!enabled))
 			return -EINVAL;
 
-	if (mask == TRACE_ITER(TRACE_PRINTK)) {
+	switch (mask) {
+	case TRACE_ITER(TRACE_PRINTK):
 		if (enabled) {
 			update_printk_trace(tr);
 		} else {
@@ -5283,9 +5286,9 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 			if (printk_trace == tr)
 				update_printk_trace(&global_trace);
 		}
-	}
+		break;
 
-	if (mask == TRACE_ITER(COPY_MARKER)) {
+	case TRACE_ITER(COPY_MARKER):
 		update_marker_trace(tr, enabled);
 		/* update_marker_trace updates the tr->trace_flags */
 		return 0;
@@ -5296,10 +5299,12 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 	else
 		tr->trace_flags &= ~mask;
 
-	if (mask == TRACE_ITER(RECORD_CMD))
+	switch (mask) {
+	case TRACE_ITER(RECORD_CMD):
 		trace_event_enable_cmd_record(enabled);
+		break;
 
-	if (mask == TRACE_ITER(RECORD_TGID)) {
+	case TRACE_ITER(RECORD_TGID):
 
 		if (trace_alloc_tgid_map() < 0) {
 			tr->trace_flags &= ~TRACE_ITER(RECORD_TGID);
@@ -5307,24 +5312,27 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 		}
 
 		trace_event_enable_tgid_record(enabled);
-	}
+		break;
 
-	if (mask == TRACE_ITER(EVENT_FORK))
+	case TRACE_ITER(EVENT_FORK):
 		trace_event_follow_fork(tr, enabled);
+		break;
 
-	if (mask == TRACE_ITER(FUNC_FORK))
+	case TRACE_ITER(FUNC_FORK):
 		ftrace_pid_follow_fork(tr, enabled);
+		break;
 
-	if (mask == TRACE_ITER(OVERWRITE)) {
+	case TRACE_ITER(OVERWRITE):
 		ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
 #ifdef CONFIG_TRACER_MAX_TRACE
 		ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
 #endif
-	}
+		break;
 
-	if (mask == TRACE_ITER(PRINTK)) {
+	case TRACE_ITER(PRINTK):
 		trace_printk_start_stop_comm(enabled);
 		trace_printk_control(enabled);
+		break;
 	}
 
 	return 0;
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] tracing: Use switch statement instead of ifs in set_tracer_flag()

Quoted 11 lines · On Wed, 05 Nov 2025 19:33:26 -0500 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

The "mask" passed in to set_trace_flag() has a single bit set. The function then checks if the mask is equal to one of the option masks and performs the appropriate function associated to that option.

Instead of having a bunch of "if ()" statement, use a "switch ()" statement instead to make it cleaner and a bit more optimal.

No function changes.

Looks good to me.

BTW, set_tracer_flag() seems to expect to modify only one bit. If we can count the number of its in @mask and reject if it is not 1, we can use bit-mask instead of the first switch()?

	if (!mask ||	/* mask has no bit */
	    (mask & ~(1 << (ffs64(mask) - 1))))	/* mask has more than 2 bits */
		return -EINVAL;

Thanks,

Quoted 102 lines
---
 kernel/trace/trace.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)
kernel/trace/trace.c +23 -15
index 07bd10808277..8460bec9f263 100644
@@ -5251,11 +5251,13 @@ int trace_keep_overwrite(struct tracer *tracer, u64 mask, int set)
 
 int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 {
-	if ((mask == TRACE_ITER(RECORD_TGID)) ||
-	    (mask == TRACE_ITER(RECORD_CMD)) ||
-	    (mask == TRACE_ITER(TRACE_PRINTK)) ||
-	    (mask == TRACE_ITER(COPY_MARKER)))
+	switch (mask) {
+	case TRACE_ITER(RECORD_TGID):
+	case TRACE_ITER(RECORD_CMD):
+	case TRACE_ITER(TRACE_PRINTK):
+	case TRACE_ITER(COPY_MARKER):
 		lockdep_assert_held(&event_mutex);
+	}
 
 	/* do nothing if flag is already set */
 	if (!!(tr->trace_flags & mask) == !!enabled)
@@ -5266,7 +5268,8 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 		if (tr->current_trace->flag_changed(tr, mask, !!enabled))
 			return -EINVAL;
 
-	if (mask == TRACE_ITER(TRACE_PRINTK)) {
+	switch (mask) {
+	case TRACE_ITER(TRACE_PRINTK):
 		if (enabled) {
 			update_printk_trace(tr);
 		} else {
@@ -5283,9 +5286,9 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 			if (printk_trace == tr)
 				update_printk_trace(&global_trace);
 		}
-	}
+		break;
 
-	if (mask == TRACE_ITER(COPY_MARKER)) {
+	case TRACE_ITER(COPY_MARKER):
 		update_marker_trace(tr, enabled);
 		/* update_marker_trace updates the tr->trace_flags */
 		return 0;
@@ -5296,10 +5299,12 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 	else
 		tr->trace_flags &= ~mask;
 
-	if (mask == TRACE_ITER(RECORD_CMD))
+	switch (mask) {
+	case TRACE_ITER(RECORD_CMD):
 		trace_event_enable_cmd_record(enabled);
+		break;
 
-	if (mask == TRACE_ITER(RECORD_TGID)) {
+	case TRACE_ITER(RECORD_TGID):
 
 		if (trace_alloc_tgid_map() < 0) {
 			tr->trace_flags &= ~TRACE_ITER(RECORD_TGID);
@@ -5307,24 +5312,27 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
 		}
 
 		trace_event_enable_tgid_record(enabled);
-	}
+		break;
 
-	if (mask == TRACE_ITER(EVENT_FORK))
+	case TRACE_ITER(EVENT_FORK):
 		trace_event_follow_fork(tr, enabled);
+		break;
 
-	if (mask == TRACE_ITER(FUNC_FORK))
+	case TRACE_ITER(FUNC_FORK):
 		ftrace_pid_follow_fork(tr, enabled);
+		break;
 
-	if (mask == TRACE_ITER(OVERWRITE)) {
+	case TRACE_ITER(OVERWRITE):
 		ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
 #ifdef CONFIG_TRACER_MAX_TRACE
 		ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
 #endif
-	}
+		break;
 
-	if (mask == TRACE_ITER(PRINTK)) {
+	case TRACE_ITER(PRINTK):
 		trace_printk_start_stop_comm(enabled);
 		trace_printk_control(enabled);
+		break;
 	}
 
 	return 0;
Signature · 1 line
2.51.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#5 lore raw

Re: [PATCH 2/2] tracing: Use switch statement instead of ifs in set_tracer_flag()

Quoted 7 lines · On Mon, 10 Nov 2025 14:48:26 +0900 Masami Hiramatsu (Google)…

BTW, set_tracer_flag() seems to expect to modify only one bit. If we can count the number of its in @mask and reject if it is not 1, we can use bit-mask instead of the first switch()?

	if (!mask ||	/* mask has no bit */
	    (mask & ~(1 << (ffs64(mask) - 1))))	/* mask has more than 2 bits */
		return -EINVAL;

Well, this has been around for over a decade without any issues. I don't think a check would be of much use. Not to mention, invalid masks are OK to pass in.

If anything, I would have liked to pass in the bit number and not a mask. But that's something we could do another time.

-- Steve