Skip to contentSkip to conversation

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

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

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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

---
 scripts/checkpatch.pl | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
scripts/checkpatch.pl +16 -0
index 0492d6afc9a1..2706aba6dac0 100755
@@ -7804,6 +7804,22 @@ sub process {
 			ERROR("UNINITIALIZED_PTR_WITH_FREE",
 			      "pointer '$1' with __free attribute should be initialized\n" . $herecurr);
 		}
+
+# check alloc_workqueue() flags parameter (2nd argument) for WQ_PERCPU, WQ_UNBOUND presence
+		if ($line =~ /\balloc_workqueue\s*\(/ &&
+			$stat =~ /\balloc_workqueue\s*\(\s*[^,]+\s*,\s*([^,]+)/s) {
+			my $flags = $1;
+			my $has_percpu = $flags =~ /\bWQ_PERCPU\b/;
+			my $has_unbound = $flags =~ /\bWQ_UNBOUND\b/;
+
+			if ($has_percpu && $has_unbound) {
+				ERROR("ALLOC_WORKQUEUE_FLAGS",
+				      "alloc_workqueue() second parameter should not contain both WQ_PERCPU and WQ_UNBOUND\n" . $herecurr);
+			} elsif (!$has_percpu && !$has_unbound) {
+				ERROR("ALLOC_WORKQUEUE_FLAGS",
+				      "alloc_workqueue() second parameter should specify either WQ_PERCPU or WQ_UNBOUND\n" . $herecurr);
+			}
+		}
 	}
 
 	# If we have no input at all, then there is nothing to report on
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 7 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.

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

[]

scripts/checkpatch.pl

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

[]

Quoted 11 lines
scripts/checkpatch.pl +7 -0
@@ -7804,6 +7804,22 @@ sub process {
 			ERROR("UNINITIALIZED_PTR_WITH_FREE",
 			      "pointer '$1' with __free attribute should be initialized\n" . $herecurr);
 		}
+
+# check alloc_workqueue() flags parameter (2nd argument) for WQ_PERCPU, WQ_UNBOUND presence
+		if ($line =~ /\balloc_workqueue\s*\(/ &&
+			$stat =~ /\balloc_workqueue\s*\(\s*[^,]+\s*,\s*([^,]+)/s) {
+			my $flags = $1;
+			my $has_percpu = $flags =~ /\bWQ_PERCPU\b/;
+			my $has_unbound = $flags =~ /\bWQ_UNBOUND\b/;

scripts/checkpatch.pl sub process { · line 7823

Quoted 10 lines
scripts/checkpatch.pl +9 -0
+
+			if ($has_percpu && $has_unbound) {
+				ERROR("ALLOC_WORKQUEUE_FLAGS",
+				      "alloc_workqueue() second parameter should not contain both WQ_PERCPU and WQ_UNBOUND\n" . $herecurr);
+			} elsif (!$has_percpu && !$has_unbound) {
+				ERROR("ALLOC_WORKQUEUE_FLAGS",
+				      "alloc_workqueue() second parameter should specify either WQ_PERCPU or WQ_UNBOUND\n" . $herecurr);
+			}
+		}
 	}

OK but a couple comments:

Add devm_alloc_workqueue as well?

Avoid using $line here as $stat isn't populated on multi-line uses of alloc_workqueue. Just test $stat

Using the patch below gives a few matches treewide with a few false positives as the flags are stored in a variable in drivers/nvme/host/... and fs/btrfs/...

-----------------
block/blk-zoned.c
-----------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1926: FILE: block/blk-zoned.c:1926:
+ disk->zone_wplugs_wq =
+ alloc_workqueue("%s_zwplugs", WQ_MEM_RECLAIM | WQ_HIGHPRI,
+ pool_size, disk->disk_name);

-----------------------------------------
drivers/gpu/drm/bridge/analogix/anx7625.c
-----------------------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #2851: FILE: drivers/gpu/drm/bridge/analogix/anx7625.c:2851:
+ platform->workqueue = alloc_workqueue("anx7625_work",
+ WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);

-------------------------------------
drivers/gpu/drm/nouveau/nouveau_drm.c
-------------------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #635: FILE: drivers/gpu/drm/nouveau/nouveau_drm.c:635:
+ drm->sched_wq = alloc_workqueue("nouveau_sched_wq_shared", 0,
+ WQ_MAX_ACTIVE);

---------------------------------------
drivers/gpu/drm/nouveau/nouveau_sched.c
---------------------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #417: FILE: drivers/gpu/drm/nouveau/nouveau_sched.c:417:
+ wq = alloc_workqueue("nouveau_sched_wq_%d", 0, WQ_MAX_ACTIVE,
+ current->pid);

------------------------------------------
drivers/media/pci/ddbridge/ddbridge-core.c
------------------------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #3433: FILE: drivers/media/pci/ddbridge/ddbridge-core.c:3433:
+ ddb_wq = alloc_workqueue("ddbridge", 0, 0);

-------------------------------------
drivers/net/wireless/ath/ath6kl/usb.c
-------------------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #639: FILE: drivers/net/wireless/ath/ath6kl/usb.c:639:
+ ar_usb->wq = alloc_workqueue("ath6kl_wq", 0, 0);

------------------------
drivers/nvme/host/core.c
------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #5410: FILE: drivers/nvme/host/core.c:5410:
+ nvme_wq = alloc_workqueue("nvme-wq", wq_flags, 0);

ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #5414: FILE: drivers/nvme/host/core.c:5414:
+ nvme_reset_wq = alloc_workqueue("nvme-reset-wq", wq_flags, 0);

ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #5418: FILE: drivers/nvme/host/core.c:5418:
+ nvme_delete_wq = alloc_workqueue("nvme-delete-wq", wq_flags, 0);

---------------------
drivers/rapidio/rio.c
---------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1997: FILE: drivers/rapidio/rio.c:1997:
+ rio_wq = alloc_workqueue("riodisc", 0, 0);

------------------------------
drivers/thermal/thermal_core.c
------------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1892: FILE: drivers/thermal/thermal_core.c:1892:
+ thermal_wq = alloc_workqueue("thermal_events", WQ_POWER_EFFICIENT, 0);

-------------------------
drivers/virt/acrn/irqfd.c
-------------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #209: FILE: drivers/virt/acrn/irqfd.c:209:
+ vm->irqfd_wq = alloc_workqueue("acrn_irqfd-%u", 0, 0, vm->vmid);

------------------
fs/btrfs/disk-io.c
------------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1950: FILE: fs/btrfs/disk-io.c:1950:
+ fs_info->endio_workers =
+ alloc_workqueue("btrfs-endio", flags, max_active);

ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1952: FILE: fs/btrfs/disk-io.c:1952:
+ fs_info->endio_meta_workers =
+ alloc_workqueue("btrfs-endio-meta", flags, max_active);

ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1954: FILE: fs/btrfs/disk-io.c:1954:
+ fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);

----------------
fs/btrfs/scrub.c
----------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #3050: FILE: fs/btrfs/scrub.c:3050:
+ scrub_workers = alloc_workqueue("btrfs-scrub", flags, max_active);

---------------
fs/ntfs/super.c
---------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #2652: FILE: fs/ntfs/super.c:2652:
+ ntfs_wq = alloc_workqueue("ntfs-bg-io", 0, 0);

---
 scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
scripts/checkpatch.pl +25 -0
index 0492d6afc9a1..aed83ce4b552 100755
@@ -7804,6 +7804,31 @@ sub process {
 			ERROR("UNINITIALIZED_PTR_WITH_FREE",
 			      "pointer '$1' with __free attribute should be initialized\n" . $herecurr);
 		}
+
+# check alloc_workqueue() and devm_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
#2 lore raw

Hi,

Quoted 4 lines · Joe Perches

[...]
OK but a couple comments:

Add devm_alloc_workqueue as well?

No I don't think it would be useful / needed. There is only 1 user of that macro, devm_alloc_ordered_workqueue(), which already specifies the WQ_UNBOUND flag and it is part of the workqueue code.

Also, we're adding this in checkpatch, because it reflects what is going to be present in the workqueue code:

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

Quoted 2 lines

Avoid using $line here as $stat isn't populated on multi-line uses of alloc_workqueue. Just test $stat

Aha, noted. I will remove that test.

Quoted 3 lines

Using the patch below gives a few matches treewide with a few false positives as the flags are stored in a variable in drivers/nvme/host/... and fs/btrfs/...

Yeah, those false positives were expected. I thought about adding a message about that, indeed.

Review

Quoted 8 lines

-----------------
block/blk-zoned.c
-----------------
ERROR: alloc_workqueue() must specify either WQ_PERCPU or WQ_UNBOUND #1926: FILE: block/blk-zoned.c:1926:

(quoted diff) +3 -0
+       disk->zone_wplugs_wq =
+               alloc_workqueue("%s_zwplugs", WQ_MEM_RECLAIM | WQ_HIGHPRI,
+                               pool_size, disk->disk_name);

Interestingly, my Coccinelle script didn't match this.

Quoted 29 lines
 scripts/checkpatch.pl | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
scripts/checkpatch.pl +18 -0
index 0492d6afc9a1..aed83ce4b552 100755
@@ -7804,6 +7804,31 @@ sub process {
                        ERROR("UNINITIALIZED_PTR_WITH_FREE",
                              "pointer '$1' with __free attribute should be initialized\n" . $herecurr);
                }
+
+# check alloc_workqueue() and devm_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)) {

scripts/checkpatch.pl sub process { · line 7834

Quoted 10 lines
scripts/checkpatch.pl +7 -0
+                               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

I will integrate my code with this!

Thanks!

Signature · 3 lines
Marco Crivellari

SUSE Labs
maintainer
#3 lore raw
Quoted 9 lines · Marco Crivellari

On Thu, Jun 4, 2026 at 7:08 AM Joe Perches <[[email protected]](mailto:[email protected])> wrote:
[...]

Quoted 3 lines

OK but a couple comments:

Add devm_alloc_workqueue as well?

No I don't think it would be useful / needed. There is only 1 user of that macro, devm_alloc_ordered_workqueue(), which already specifies the WQ_UNBOUND flag and it is part of the workqueue code.

drivers/power/supply/ipaq_micro_battery.c: mb->wq = devm_alloc_workqueue(&pdev->dev, "ipaq-battery-wq",

Maybe there will be more.

#4 lore raw
Quoted 8 lines · Joe Perches
Quoted 6 lines · Marco Crivellari

On Thu, Jun 4, 2026 at 7:08 AM Joe Perches <[[email protected]](mailto:[email protected])> wrote:
[...]

Quoted 3 lines

OK but a couple comments:

Add devm_alloc_workqueue as well?

Quoted 7 lines
Quoted 3 lines

No I don't think it would be useful / needed. There is only 1 user of that macro, devm_alloc_ordered_workqueue(), which already specifies the WQ_UNBOUND flag and it is part of the workqueue code.

drivers/power/supply/ipaq_micro_battery.c: mb->wq = devm_alloc_workqueue(&pdev->dev, "ipaq-battery-wq",

Maybe there will be more.

Yes it is the only one in v7.1-rc6.

BTW, let's keep the new regex for both macros, it's not a problem, so we can cover both cases.

Thanks!

Signature · 3 lines
Marco Crivellari

SUSE Labs