[PATCH v2] checkpatch: Check WQ_PERCPU or WQ_UNBOUND presence in alloc_workqueue() users

Marco Crivellari posted 1 patch 3 days, 15 hours ago
There is a newer version of this series
scripts/checkpatch.pl | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
[PATCH v2] checkpatch: Check WQ_PERCPU or WQ_UNBOUND presence in alloc_workqueue() users
Posted by Marco Crivellari 3 days, 15 hours ago
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

Signed-off-by: Marco Crivellari <marco.crivellari@suse.com>
---
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/20260603140941.320063-1-marco.crivellari@suse.com/

 scripts/checkpatch.pl | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 0492d6afc9a1..2464d83619f7 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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);
+				ERROR("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) {
-- 
2.54.0
Re: [PATCH v2] checkpatch: Check WQ_PERCPU or WQ_UNBOUND presence in alloc_workqueue() users
Posted by Joe Perches 3 days, 14 hours ago
On Thu, 2026-06-04 at 17:44 +0200, Marco Crivellari wrote:
> 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
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -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)/) {

Align to open paren please

> +
> +			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);
> +				ERROR("ALLOC_WORKQUEUE_FLAGS",
> +				  $error_msg . $herectx);

Align to open paren please

And as there are false positives, this should probably be a WARN
Re: [PATCH v2] checkpatch: Check WQ_PERCPU or WQ_UNBOUND presence in alloc_workqueue() users
Posted by Marco Crivellari 2 days, 23 hours ago
On Thu, Jun 4, 2026 at 7:11 PM Joe Perches <joe@perches.com> wrote:
> [...]
> Align to open paren please
>
> And as there are false positives, this should probably be a WARN

Ok, I will send the v3 with these changes.

Thanks!

-- 

Marco Crivellari

SUSE Labs