Skip to contentSkip to conversation

checkpatch: Check WQ_PERCPU or WQ_UNBOUND presence in alloc_workqueue() users

PATCH Superseded Marco Crivellari 2 participants 3 messages Jun 5 – Jun 8 Checkpatch

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 checkpatch: Check WQ_PERCPU or WQ_UNBOUND presence in alloc_workqueue() users

The workqueue API introduced a new flag, WQ_PERCPU, that has to be used when WQ_UNBOUND is not present. One of these flags must be present, but not both of them.

To limit usage mistakes, emit an ERROR if one of the below condition is met:
- alloc_workqueue() is called without WQ_PERCPU nor WQ_UNBOUND
- alloc_workqueue() is called with both WQ_PERCPU and WQ_UNBOUND

---
Changes in v3:
- code aligned to open parens

- ERROR changed with WARN

Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- removed $line from the test (Joe Perches)

- devm_alloc_workqueue() regex take into account also (Joe Perches)

- defined() on variables and other improvements (Joe Perches)

Link to v1: https://lore.kernel.org/all/[email protected]/

 scripts/checkpatch.pl | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +24 -1
index 0492d6afc9a1..f1b03897b6e7 100755
@@ -7804,8 +7804,31 @@ sub process {
 			ERROR("UNINITIALIZED_PTR_WITH_FREE",
 			      "pointer '$1' with __free attribute should be initialized\n" . $herecurr);
 		}
-	}
 
+# check alloc_workqueue() parameters for WQ_PERCPU and WQ_UNBOUND uses
+		if (defined($stat) &&
+			$stat =~ /^[ \+]\s*(?:$Lval\s*=\s*)?((?:devm_)?alloc_workqueue)\s*($balanced_parens)/) {
+
+			my $func = $1;
+			my $args = $2;
+			my $has_percpu = $args =~ /\bWQ_PERCPU\b/;
+			my $has_unbound = $args =~ /\bWQ_UNBOUND\b/;
+			my $error_msg;
+
+			if ($has_percpu && $has_unbound) {
+				$error_msg = "$func() should not contain both WQ_PERCPU and WQ_UNBOUND\n";
+			} elsif (!$has_percpu && !$has_unbound) {
+				$error_msg = "$func() must specify either WQ_PERCPU or WQ_UNBOUND\n";
+			}
+
+			if (defined($error_msg)) {
+				my $stmt_cnt = statement_rawlines($stat);
+				my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
+				WARN("ALLOC_WORKQUEUE_FLAGS",
+					 $error_msg . $herectx);
+			}
+		}
+	}
 	# If we have no input at all, then there is nothing to report on
 	# so just keep quiet.
 	if ($#rawlines == -1) {
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 3 lines · Marco Crivellari

The workqueue API introduced a new flag, WQ_PERCPU, that has to be used when WQ_UNBOUND is not present. One of these flags must be present, but not both of them.

[]

Quoted 4 lines

---
Changes in v3:
- code aligned to open parens

Not so.

scripts/checkpatch.pl

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

[]

scripts/checkpatch.pl sub process { · line 7810

Quoted 9 lines
scripts/checkpatch.pl +3 -1
@@ -7804,8 +7804,31 @@ sub process {
 			ERROR("UNINITIALIZED_PTR_WITH_FREE",
 			      "pointer '$1' with __free attribute should be initialized\n" . $herecurr);
 		}
-	}
 
+# check alloc_workqueue() parameters for WQ_PERCPU and WQ_UNBOUND uses
+		if (defined($stat) &&
+			$stat =~ /^[ \+]\s*(?:$Lval\s*=\s*)?((?:devm_)?alloc_workqueue)\s*($balanced_parens)/) {

Properly aligned would be 2 tabs then 4 spaces

		    $stat =~ /^[ \+]\s*(?:$Lval\s*=\s*)?((?:devm_)?alloc_workqueue)\s*($balanced_parens)/) {

[]

scripts/checkpatch.pl sub process { · line 7828

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+			if (defined($error_msg)) {
+				my $stmt_cnt = statement_rawlines($stat);
+				my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
+				WARN("ALLOC_WORKQUEUE_FLAGS",
+					 $error_msg . $herectx);

4 tabs then 5 spaces

#2 lore raw
Quoted 5 lines · Joe Perches
Quoted 3 lines · Marco Crivellari

The workqueue API introduced a new flag, WQ_PERCPU, that has to be used when WQ_UNBOUND is not present. One of these flags must be present, but not both of them.

Quoted 7 lines

[]

Quoted 4 lines

---
Changes in v3:
- code aligned to open parens

Not so.

Aha, thanks, I will send a new version.

Signature · 3 lines
Marco Crivellari

SUSE Labs