Skip to contentSkip to conversation

workqueue: Fix incorrect error return value in apply_workqueue_attrs_locked

PATCH In review Ye Liu 3 participants 5 messages Mar 24, 2025 – Mar 25, 2025

Subsystems (2)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH 1/1 workqueue: Fix incorrect error return value in apply_workqueue_attrs_locked

From: Ye Liu <[email protected]>

Commit 84193c07105c ("workqueue: Generalize unbound CPU pods") introduced a change that caused apply_workqueue_attrs_locked() to return error pointers using PTR_ERR() on failure instead of a negative error code. This caused unexpected behavior in functions that rely on the return value of apply_workqueue_attrs_locked, such as alloc_and_link_pwqs().

Specifically, alloc_and_link_pwqs() expects apply_workqueue_attrs_locked() to return 0 on success and a negative error code on failure. However, returning PTR_ERR(ctx) instead of -ENOMEM led to incorrect error handling in __alloc_workqueue, potentially causing system instability or crashes.

This patch ensures apply_workqueue_attrs_locked() returns a proper negative error code (-ENOMEM) in case of failure, restoring expected behavior.

Fixes 84193c07105c ("workqueue: Generalize unbound CPU pods")
---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +1 -1
index bfe030b443e2..8ba679d9b566 100644
@@ -5363,7 +5363,7 @@ static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
 
 	ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask);
 	if (IS_ERR(ctx))
-		return PTR_ERR(ctx);
+		return -ENOMEM;
 
 	/* the ctx has been prepared successfully, let's commit it */
 	apply_wqattrs_commit(ctx);
Signature · 1 line
2.25.1
#3 lore raw
In reply to Ye Liu

On Mon, Mar 24, 2025 at 05:07:48PM +0800, Ye Liu wrote:
v> From: Ye Liu <[email protected]>

Quoted 4 lines

Commit 84193c07105c ("workqueue: Generalize unbound CPU pods") introduced a change that caused apply_workqueue_attrs_locked() to return error pointers using PTR_ERR() on failure instead of a negative error code.

PTR_ERR() does return negative error codes. Unless you pass it a NULL pointer, then it returns success. Or if you pass it a valid pointer it returns garbage.

Quoted 22 lines

This caused unexpected behavior in functions that rely on the return value of apply_workqueue_attrs_locked, such as alloc_and_link_pwqs().

Specifically, alloc_and_link_pwqs() expects apply_workqueue_attrs_locked() to return 0 on success and a negative error code on failure. However, returning PTR_ERR(ctx) instead of -ENOMEM led to incorrect error handling in __alloc_workqueue, potentially causing system instability or crashes.

This patch ensures apply_workqueue_attrs_locked() returns a proper negative error code (-ENOMEM) in case of failure, restoring expected behavior.

Fixes 84193c07105c ("workqueue: Generalize unbound CPU pods")
---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +0 -0
index bfe030b443e2..8ba679d9b566 100644

kernel/workqueue.c static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, · line 5367

Quoted 6 lines
kernel/workqueue.c +1 -1
@@ -5363,7 +5363,7 @@ static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
 
 	ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask);
 	if (IS_ERR(ctx))
-		return PTR_ERR(ctx);
+		return -ENOMEM;
 

The original code was correct and the patch is wrong.

regards,
dan carpenter

author
#4 lore raw
Quoted 8 lines · Dan Carpenter

On Mon, Mar 24, 2025 at 05:07:48PM +0800, Ye Liu wrote:
v> From: Ye Liu <[email protected]>

Quoted 3 lines

Commit 84193c07105c ("workqueue: Generalize unbound CPU pods") introduced a change that caused apply_workqueue_attrs_locked() to return error pointers using PTR_ERR() on failure instead of a negative error code.

PTR_ERR() does return negative error codes. Unless you pass it a NULL pointer, then it returns success. Or if you pass it a valid pointer it returns garbage.

I misunderstood PTR_ERR(). Thanks for pointing it out.

Quoted 23 lines
Quoted 23 lines

This caused unexpected behavior in functions that rely on the return value of apply_workqueue_attrs_locked, such as alloc_and_link_pwqs().

Specifically, alloc_and_link_pwqs() expects apply_workqueue_attrs_locked() to return 0 on success and a negative error code on failure. However, returning PTR_ERR(ctx) instead of -ENOMEM led to incorrect error handling in __alloc_workqueue, potentially causing system instability or crashes.

This patch ensures apply_workqueue_attrs_locked() returns a proper negative error code (-ENOMEM) in case of failure, restoring expected behavior.

Fixes 84193c07105c ("workqueue: Generalize unbound CPU pods")
---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +0 -0
index bfe030b443e2..8ba679d9b566 100644
@@ -5363,7 +5363,7 @@ static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
 
Quoted 6 lines

kernel/workqueue.c static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, · line 5367

Quoted 5 lines
kernel/workqueue.c +1 -1
 	ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask);
 	if (IS_ERR(ctx))
-		return PTR_ERR(ctx);
+		return -ENOMEM;
 

The original code was correct and the patch is wrong.

Drop it.

Quoted 3 lines

regards,
dan carpenter

Thanks,

Ye Liu