Skip to contentSkip to conversation

tracing user_events: Simplify user_event_parse_field() parsing

PATCH In review Steven Rostedt 2 participants 4 messages Jan 8, 2024 Tracing

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH 1/1 tracing user_events: Simplify user_event_parse_field() parsing

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

Instead of having a bunch of if statements with:

       len = str_has_prefix(field, "__data_loc unsigned ");
       if (len)
               goto skip_next;

       len = str_has_prefix(field, "__data_loc ");
       if (len)
               goto skip_next;

       len = str_has_prefix(field, "__rel_loc unsigned ");
       if (len)
               goto skip_next;

       len = str_has_prefix(field, "__rel_loc ");
       if (len)
               goto skip_next;

	goto parse;

skip_next:

Consolidate it into a negative check and jump to parse if all the str_has_prefix() calls fail. If one succeeds, it will just continue with len equal to the proper string:

       if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&
           !(len = str_has_prefix(field, "__data_loc ")) &&
           !(len = str_has_prefix(field, "__rel_loc unsigned ")) &&
           !(len = str_has_prefix(field, "__rel_loc "))) {
               goto parse;
       }

skip_next:

---
 kernel/trace/trace_events_user.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
kernel/trace/trace_events_user.c +6 -16
index 9365ce407426..ce0c5f1ded48 100644
@@ -1175,23 +1175,13 @@ static int user_event_parse_field(char *field, struct user_event *user,
 		goto skip_next;
 	}
 
-	len = str_has_prefix(field, "__data_loc unsigned ");
-	if (len)
-		goto skip_next;
-
-	len = str_has_prefix(field, "__data_loc ");
-	if (len)
-		goto skip_next;
-
-	len = str_has_prefix(field, "__rel_loc unsigned ");
-	if (len)
-		goto skip_next;
-
-	len = str_has_prefix(field, "__rel_loc ");
-	if (len)
-		goto skip_next;
+	if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&
+	    !(len = str_has_prefix(field, "__data_loc ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc unsigned ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc "))) {
+		goto parse;
+	}
 
-	goto parse;
 skip_next:
 	type = field;
 	field = strpbrk(field + len, " ");
Signature · 1 line
2.43.0
#1 lore raw
Quoted 61 lines · Steven Rostedt

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

Instead of having a bunch of if statements with:

       len = str_has_prefix(field, "__data_loc unsigned ");
       if (len)
               goto skip_next;

       len = str_has_prefix(field, "__data_loc ");
       if (len)
               goto skip_next;

       len = str_has_prefix(field, "__rel_loc unsigned ");
       if (len)
               goto skip_next;

       len = str_has_prefix(field, "__rel_loc ");
       if (len)
               goto skip_next;

	goto parse;

skip_next:

Consolidate it into a negative check and jump to parse if all the str_has_prefix() calls fail. If one succeeds, it will just continue with len equal to the proper string:

       if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&
           !(len = str_has_prefix(field, "__data_loc ")) &&
           !(len = str_has_prefix(field, "__rel_loc unsigned ")) &&
           !(len = str_has_prefix(field, "__rel_loc "))) {
               goto parse;
       }

skip_next:

---
 kernel/trace/trace_events_user.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
kernel/trace/trace_events_user.c +0 -11
index 9365ce407426..ce0c5f1ded48 100644
@@ -1175,23 +1175,13 @@ static int user_event_parse_field(char *field, struct user_event *user,
 		goto skip_next;
 	}
 
-	len = str_has_prefix(field, "__data_loc unsigned ");
-	if (len)
-		goto skip_next;
-
-	len = str_has_prefix(field, "__data_loc ");
-	if (len)
-		goto skip_next;
-
-	len = str_has_prefix(field, "__rel_loc unsigned ");
-	if (len)
-		goto skip_next;

kernel/trace/trace_events_user.c static int user_event_parse_field(char *field, struct user_event *user, · line 1183

Quoted 10 lines
kernel/trace/trace_events_user.c +6 -4
-
-	len = str_has_prefix(field, "__rel_loc ");
-	if (len)
-		goto skip_next;
+	if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&
+	    !(len = str_has_prefix(field, "__data_loc ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc unsigned ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc "))) {
+		goto parse;
+	}

This now triggers a checkpatch error:
ERROR: do not use assignment in if condition #1184: FILE: kernel/trace/trace_events_user.c:1184:
+ if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&

I personally prefer to keep these files fully checkpatch clean. However, I did test these changes under the self-tests and it passed.

Do they bug you that much? :)

Thanks,
-Beau

Quoted 7 lines
kernel/trace/trace_events_user.c +0 -1
-	goto parse;
 skip_next:
 	type = field;
 	field = strpbrk(field + len, " ");
Signature · 1 line
2.43.0
author maintainer
#2 lore raw
Quoted 4 lines · On Mon, 8 Jan 2024 21:47:44 +0000 Beau Belgrave <[email protected]>…
Quoted 4 lines
kernel/trace/trace_events_user.c +1 -3
-	len = str_has_prefix(field, "__rel_loc ");
-	if (len)
-		goto skip_next;
+	if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&
Quoted 8 lines

kernel/trace/trace_events_user.c static int user_event_parse_field(char *field, struct user_event *user, · line 1183

Quoted 5 lines
kernel/trace/trace_events_user.c +5 -0
+	    !(len = str_has_prefix(field, "__data_loc ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc unsigned ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc "))) {
+		goto parse;
+	}  

This now triggers a checkpatch error:
ERROR: do not use assignment in if condition

What a horrible message.

Quoted 4 lines

#1184: FILE: kernel/trace/trace_events_user.c:1184:
+ if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&

I personally prefer to keep these files fully checkpatch clean.

I've stopped using checkpatch years ago because I disagreed with so much it :-p
(Including this message)

Quoted 3 lines

However, I did test these changes under the self-tests and it passed.

Do they bug you that much? :)

No big deal if you prefer the other way. I was just doing an audit of str_has_prefix() to see what code could be cleaned up that uses it, and I found this code.

If you prefer to limit your code to "checkpatch clean", I'll leave it alone.

-- Steve

author maintainer
#3 lore raw
Quoted 18 lines · On Mon, 8 Jan 2024 17:13:12 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Mon, 8 Jan 2024 21:47:44 +0000 Beau Belgrave <[email protected]>…
Quoted 4 lines
kernel/trace/trace_events_user.c +1 -3
-	len = str_has_prefix(field, "__rel_loc ");
-	if (len)
-		goto skip_next;
+	if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&
Quoted 8 lines

kernel/trace/trace_events_user.c static int user_event_parse_field(char *field, struct user_event *user, · line 1183

Quoted 5 lines
kernel/trace/trace_events_user.c +5 -0
+	    !(len = str_has_prefix(field, "__data_loc ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc unsigned ")) &&
+	    !(len = str_has_prefix(field, "__rel_loc "))) {
+		goto parse;
+	}    

This now triggers a checkpatch error:
ERROR: do not use assignment in if condition

What a horrible message.

Quoted 7 lines
Quoted 4 lines

#1184: FILE: kernel/trace/trace_events_user.c:1184:
+ if (!(len = str_has_prefix(field, "__data_loc unsigned ")) &&

I personally prefer to keep these files fully checkpatch clean.

I've stopped using checkpatch years ago because I disagreed with so much it :-p
(Including this message)

Note that checkpatch is a guideline and not a rule. The general rule is, if the code looks worse when applying the checkpatch rule, don't do it.

- Steve