Skip to contentSkip to conversation

checkpatch: Warn about data_race() without comment

PATCH In review Marco Elver 4 participants 7 messages Apr 1, 2020 – Apr 16, 2020 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: Warn about data_race() without comment

Warn about applications of data_race() without a comment, to encourage documenting the reasoning behind why it was deemed safe.

---
 scripts/checkpatch.pl | 8 ++++++++
 1 file changed, 8 insertions(+)
scripts/checkpatch.pl +8 -0
index a63380c6b0d2..48bb9508e300 100755
@@ -5833,6 +5833,14 @@ sub process {
 			}
 		}
 
+# check for data_race without a comment.
+		if ($line =~ /\bdata_race\s*\(/) {
+			if (!ctx_has_comment($first_line, $linenr)) {
+				WARN("DATA_RACE",
+				     "data_race without comment\n" . $herecurr);
+			}
+		}
+
 # check for smp_read_barrier_depends and read_barrier_depends
 		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
 			WARN("READ_BARRIER_DEPENDS",
Signature · 1 line
2.26.0.rc2.310.g2932bb562d-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Marco Elver

Warn about applications of data_race() without a comment, to encourage documenting the reasoning behind why it was deemed safe.

---
 scripts/checkpatch.pl | 8 ++++++++
 1 file changed, 8 insertions(+)
scripts/checkpatch.pl +0 -0
index a63380c6b0d2..48bb9508e300 100755
@@ -5833,6 +5833,14 @@ sub process {
 			}

scripts/checkpatch.pl sub process { · line 5843

Quoted 10 lines
scripts/checkpatch.pl +8 -0
 		}
 
+# check for data_race without a comment.
+		if ($line =~ /\bdata_race\s*\(/) {
+			if (!ctx_has_comment($first_line, $linenr)) {
+				WARN("DATA_RACE",
+				     "data_race without comment\n" . $herecurr);
+			}
+		}
+

Thanks, looks sane to me:

Although I suppose I now need to add some comments to my list stuff. I didn't think that through, did I? ;)

Will

maintainer
#2 lore raw
In reply to Marco Elver
Quoted 2 lines · Marco Elver

Warn about applications of data_race() without a comment, to encourage documenting the reasoning behind why it was deemed safe.

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 5 lines
scripts/checkpatch.pl +1 -0
@@ -5833,6 +5833,14 @@ sub process {
 			}
 		}
 
+# check for data_race without a comment.

scripts/checkpatch.pl sub process { · line 5846

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+		if ($line =~ /\bdata_race\s*\(/) {
+			if (!ctx_has_comment($first_line, $linenr)) {
+				WARN("DATA_RACE",
+				     "data_race without comment\n" . $herecurr);
+			}
+		}
+
 # check for smp_read_barrier_depends and read_barrier_depends
 		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
 			WARN("READ_BARRIER_DEPENDS",

Sensible enough but it looks like ctx_has_comment should be updated to allow c99 comments too, but that should be a separate change from this patch.

Otherwise, this style emits a message:

WARNING: data_race without comment
#135: FILE: kernel/rcu/tasks.h:135:
+ int i = data_race(rtp->gp_state); // Let KCSAN detect update races

#3 lore raw
Quoted 31 lines · Joe Perches
Quoted 2 lines · Marco Elver

Warn about applications of data_race() without a comment, to encourage documenting the reasoning behind why it was deemed safe.

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 5 lines
scripts/checkpatch.pl +1 -0
@@ -5833,6 +5833,14 @@ sub process {
 			}
 		}
 
+# check for data_race without a comment.

scripts/checkpatch.pl sub process { · line 5846

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+		if ($line =~ /\bdata_race\s*\(/) {
+			if (!ctx_has_comment($first_line, $linenr)) {
+				WARN("DATA_RACE",
+				     "data_race without comment\n" . $herecurr);
+			}
+		}
+
 # check for smp_read_barrier_depends and read_barrier_depends
 		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
 			WARN("READ_BARRIER_DEPENDS",

Sensible enough but it looks like ctx_has_comment should be updated to allow c99 comments too, but that should be a separate change from this patch.

Otherwise, this style emits a message:

WARNING: data_race without comment
#135: FILE: kernel/rcu/tasks.h:135:

Review

Quoted 1 line
(quoted diff) +1 -0
+	int i = data_race(rtp->gp_state); // Let KCSAN detect update races

Yes, please!

Thanx, Paul

maintainer
#4 lore raw

PATCH 1/1 checkpatch: Look for c99 comments in ctx_locate_comment

Some checks look for comments around a specific function like read_barrier_depends.

Extend the check to support both c89 and c90 comment styles.

	c89 /* comment */
or
	c99 // comment

For c99 comments, only look a 3 single lines, the line being scanned, the line above and the line below the line being scanned rather than the patch diff context.

---
 scripts/checkpatch.pl | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +9 -1
index d64c67..0f4db4 100755
@@ -1674,8 +1674,16 @@ sub ctx_statement_level {
 sub ctx_locate_comment {
 	my ($first_line, $end_line) = @_;
 
+	# If c99 comment on the current line, or the line before or after
+	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
+	return $current_comment if (defined $current_comment);
+	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
+	return $current_comment if (defined $current_comment);
+	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
+	return $current_comment if (defined $current_comment);
+
 	# Catch a comment on the end of the line itself.
-	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
+	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
 	return $current_comment if (defined $current_comment);
 
 	# Look through the context and try and figure out if there is a
Tested-by on v1 1/1
#5 lore raw

Re: [PATCH] checkpatch: Look for c99 comments in ctx_locate_comment

Quoted 9 lines · Joe Perches

Some checks look for comments around a specific function like read_barrier_depends.

Extend the check to support both c89 and c90 comment styles.

	c89 /* comment */
or
	c99 // comment
Quoted 5 lines

For c99 comments, only look a 3 single lines, the line being scanned, the line above and the line below the line being scanned rather than the patch diff context.

Quoted 28 lines
---
 scripts/checkpatch.pl | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +9 -1
index d64c67..0f4db4 100755
@@ -1674,8 +1674,16 @@ sub ctx_statement_level {
 sub ctx_locate_comment {
 	my ($first_line, $end_line) = @_;
 
+	# If c99 comment on the current line, or the line before or after
+	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
+	return $current_comment if (defined $current_comment);
+	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
+	return $current_comment if (defined $current_comment);
+	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
+	return $current_comment if (defined $current_comment);
+
 	# Catch a comment on the end of the line itself.
-	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
+	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
 	return $current_comment if (defined $current_comment);
 
 	# Look through the context and try and figure out if there is a
#6 lore raw
In reply to Joe Perches
Quoted 11 lines · Joe Perches
Quoted 2 lines · Marco Elver

Warn about applications of data_race() without a comment, to encourage documenting the reasoning behind why it was deemed safe.

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -5833,6 +5833,14 @@ sub process {
                      }
              }
 
Quoted 11 lines

scripts/checkpatch.pl sub process { · line 5846

Quoted 11 lines
scripts/checkpatch.pl +8 -0
+# check for data_race without a comment.
+             if ($line =~ /\bdata_race\s*\(/) {
+                     if (!ctx_has_comment($first_line, $linenr)) {
+                             WARN("DATA_RACE",
+                                  "data_race without comment\n" . $herecurr);
+                     }
+             }
+
 # check for smp_read_barrier_depends and read_barrier_depends
              if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
                      WARN("READ_BARRIER_DEPENDS",

Do we still want to do this? Which tree can pick this up? Or was there anything left that we missed?

Quoted 3 lines

Sensible enough but it looks like ctx_has_comment should be updated to allow c99 comments too, but that should be a separate change from this patch.

AFAIK the C99 comment patch is in -mm now.

Quoted 6 lines

Otherwise, this style emits a message:

WARNING: data_race without comment
#135: FILE: kernel/rcu/tasks.h:135:
+ int i = data_race(rtp->gp_state); // Let KCSAN detect update races

Thanks,
-- Marco