[PATCH 1/2] workqueue: Allow to expose ordered workqueues via sysfs

Sebastian Andrzej Siewior posted 2 patches 3 days, 22 hours ago
[PATCH 1/2] workqueue: Allow to expose ordered workqueues via sysfs
Posted by Sebastian Andrzej Siewior 3 days, 22 hours ago
Ordered workqueues are not exposed via sysfs because the 'max_active'
attribute changes the number actives worker. More than one active worker
can break ordering guarantees.

This can be avoided by forbidding writes the file for ordered
workqueues. Exposing it via sysfs allows to alter other attributes such
as the cpumask on which CPU the worker can run.

The 'max_active' value shouldn't be changed for BH worker because the
core never spawns additional worker and the worker itself can not be
preempted. So this make no sense.

Allow to expose ordered workqueues via sysfs if requested and forbid
changing 'max_active' value for ordered and BH worker.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/workqueue.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 253311af47c6d..625ee8cc47f40 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -7097,6 +7097,13 @@ static ssize_t max_active_store(struct device *dev,
 	struct workqueue_struct *wq = dev_to_wq(dev);
 	int val;
 
+	/*
+	 * Adjusting max_active breaks ordering guarantee. Changing it has no
+	 * effect on BH worker.
+	 */
+	if (wq->flags & (WQ_BH | __WQ_ORDERED))
+		return -EACCES;
+
 	if (sscanf(buf, "%d", &val) != 1 || val <= 0)
 		return -EINVAL;
 
@@ -7413,13 +7420,6 @@ int workqueue_sysfs_register(struct workqueue_struct *wq)
 	struct wq_device *wq_dev;
 	int ret;
 
-	/*
-	 * Adjusting max_active breaks ordering guarantee.  Disallow exposing
-	 * ordered workqueues.
-	 */
-	if (WARN_ON(wq->flags & __WQ_ORDERED))
-		return -EINVAL;
-
 	wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
 	if (!wq_dev)
 		return -ENOMEM;
-- 
2.51.0
Re: [PATCH 1/2] workqueue: Allow to expose ordered workqueues via sysfs
Posted by Sebastian Andrzej Siewior 3 days, 21 hours ago
On 2026-02-05 12:55:58 [+0100], To linux-efi@vger.kernel.org wrote:
> --- a/kernel/workqueue.c
> +++ b/kernel/workqueue.c
> @@ -7097,6 +7097,13 @@ static ssize_t max_active_store(struct device *dev,
>  	struct workqueue_struct *wq = dev_to_wq(dev);
>  	int val;
>  
> +	/*
> +	 * Adjusting max_active breaks ordering guarantee. Changing it has no
> +	 * effect on BH worker.
> +	 */
> +	if (wq->flags & (WQ_BH | __WQ_ORDERED))
> +		return -EACCES;
> +
>  	if (sscanf(buf, "%d", &val) != 1 || val <= 0)
>  		return -EINVAL;

I have been informed that instead of this -EACCES I could do the
following and exposing only the max_active (and per_cpu) attribute as
RO instead.

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 625ee8cc47f40..793b59ce99edb 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -7097,13 +7097,6 @@ static ssize_t max_active_store(struct device *dev,
 	struct workqueue_struct *wq = dev_to_wq(dev);
 	int val;
 
-	/*
-	 * Adjusting max_active breaks ordering guarantee. Changing it has no
-	 * effect on BH worker.
-	 */
-	if (wq->flags & (WQ_BH | __WQ_ORDERED))
-		return -EACCES;
-
 	if (sscanf(buf, "%d", &val) != 1 || val <= 0)
 		return -EINVAL;
 
@@ -7117,7 +7110,26 @@ static struct attribute *wq_sysfs_attrs[] = {
 	&dev_attr_max_active.attr,
 	NULL,
 };
-ATTRIBUTE_GROUPS(wq_sysfs);
+
+static umode_t wq_sysfs_is_visible(struct kobject *kobj, struct attribute *a, int n)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct workqueue_struct *wq = dev_to_wq(dev);
+
+	/*
+	 * Adjusting max_active breaks ordering guarantee. Changing it has no
+	 * effect on BH worker. Limit max_active to RO in such case.
+	 */
+	if (wq->flags & (WQ_BH | __WQ_ORDERED))
+		return 0444;
+	return a->mode;
+}
+
+static const struct attribute_group wq_sysfs_group = {
+	.is_visible = wq_sysfs_is_visible,
+	.attrs = wq_sysfs_attrs,
+};
+__ATTRIBUTE_GROUPS(wq_sysfs);
 
 static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
 			    char *buf)
Re: [PATCH 1/2] workqueue: Allow to expose ordered workqueues via sysfs
Posted by Tejun Heo 3 days, 12 hours ago
On Thu, Feb 05, 2026 at 02:39:13PM +0100, Sebastian Andrzej Siewior wrote:
> +static umode_t wq_sysfs_is_visible(struct kobject *kobj, struct attribute *a, int n)
> +{
> +	struct device *dev = kobj_to_dev(kobj);
> +	struct workqueue_struct *wq = dev_to_wq(dev);
> +
> +	/*
> +	 * Adjusting max_active breaks ordering guarantee. Changing it has no
> +	 * effect on BH worker. Limit max_active to RO in such case.
> +	 */
> +	if (wq->flags & (WQ_BH | __WQ_ORDERED))
> +		return 0444;
> +	return a->mode;
> +}
> +
> +static const struct attribute_group wq_sysfs_group = {
> +	.is_visible = wq_sysfs_is_visible,
> +	.attrs = wq_sysfs_attrs,
> +};
> +__ATTRIBUTE_GROUPS(wq_sysfs);

Yeah, this looks fine to me.

Thanks.

-- 
tejun