Skip to contentSkip to conversation

workqueue: Try to catch flush_work() without INIT_WORK().

PATCH Applied Tetsuo Handa 3 participants 7 messages Jan 18, 2019 – Jan 25, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: Try to catch flush_work() without INIT_WORK().

syzbot found a flush_work() caller who forgot to call INIT_WORK() because that work_struct was allocated by kzalloc(). But the message

INFO: trying to register non-static key.
the code is fine but needs lockdep annotation.
turning off the locking correctness validator.

by lock_map_acquire() is failing to tell that INIT_WORK() is missing.

Since flush_work() without INIT_WORK() is a bug, and INIT_WORK() should set ->func field to non-zero, let's warn if ->func field is zero.

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +3 -0
index 392be4b..a503ad9 100644
@@ -2908,6 +2908,9 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
 	if (WARN_ON(!wq_online))
 		return false;
 
+	if (WARN_ON(!work->func))
+		return false;
+
 	if (!from_cancel) {
 		lock_map_acquire(&work->lockdep_map);
 		lock_map_release(&work->lockdep_map);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 11 lines · Tetsuo Handa

syzbot found a flush_work() caller who forgot to call INIT_WORK() because that work_struct was allocated by kzalloc(). But the message

INFO: trying to register non-static key.
the code is fine but needs lockdep annotation.
turning off the locking correctness validator.

by lock_map_acquire() is failing to tell that INIT_WORK() is missing.

Since flush_work() without INIT_WORK() is a bug, and INIT_WORK() should set ->func field to non-zero, let's warn if ->func field is zero.

Agree that it's a good idea to catch this. So the caller did flush_work without queueing it beforehand? Out of curiosity, what situation leads to this? Link to the report might be helpful.

kernel/workqueue.c static bool __flush_work(struct work_struct *work, bool from_cancel) · line 2913

Quoted 11 lines
kernel/workqueue.c +3 -0
index 392be4b..a503ad9 100644
@@ -2908,6 +2908,9 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
 	if (WARN_ON(!wq_online))
 		return false;
 
+	if (WARN_ON(!work->func))
+		return false;
+

__queue_work has a sanity check already for work, but using list_empty. Seems slightly better to be consistent?

#2 lore raw
Quoted 8 lines · Daniel Jordan
Quoted 7 lines · Tetsuo Handa

syzbot found a flush_work() caller who forgot to call INIT_WORK() because that work_struct was allocated by kzalloc(). But the message

INFO: trying to register non-static key.
the code is fine but needs lockdep annotation.
turning off the locking correctness validator.

Quoted 8 lines
Quoted 4 lines

by lock_map_acquire() is failing to tell that INIT_WORK() is missing.

Since flush_work() without INIT_WORK() is a bug, and INIT_WORK() should set ->func field to non-zero, let's warn if ->func field is zero.

Agree that it's a good idea to catch this. So the caller did flush_work without queueing it beforehand? Out of curiosity, what situation leads to this? Link to the report might be helpful.

I quoted the patch below.

Quoted 9 lines
Quoted 9 lines
kernel/workqueue.c +0 -0
index 392be4b..a503ad9 100644
@@ -2908,6 +2908,9 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
 	if (WARN_ON(!wq_online))
 		return false;
 
Quoted 7 lines

kernel/workqueue.c static bool __flush_work(struct work_struct *work, bool from_cancel) · line 2913

Quoted 3 lines
kernel/workqueue.c +3 -0
+	if (WARN_ON(!work->func))
+		return false;
+

__queue_work has a sanity check already for work, but using list_empty. Seems slightly better to be consistent?

list_empty() won't work, for "struct work_struct" is embedded into a struct which is allocated by kzalloc().

From 1bbf8d9c7aaef78d7f483d2131261162485c6f72 Mon Sep 17 00:00:00 2001
From: Tetsuo Handa <[email protected]>
Date: Sat, 19 Jan 2019 01:38:53 +0900
Subject: [PATCH] drm/vkms: Fix flush_work() without INIT_WORK().

syzbot is hitting a lockdep warning [1] because flush_work() is called without INIT_WORK() after kzalloc() at vkms_atomic_crtc_reset().

Commit 6c234fe37c57627a ("drm/vkms: Implement CRC debugfs API") added INIT_WORK() to only vkms_atomic_crtc_duplicate_state() side. Assuming that lifecycle of crc_work is appropriately managed, fix this problem by adding INIT_WORK() to vkms_atomic_crtc_reset() side.

[1] https://syzkaller.appspot.com/bug?id=a5954455fcfa51c29ca2ab55b203076337e1c770

---
 drivers/gpu/drm/vkms/vkms_crtc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/vkms/vkms_crtc.c +1 -0
index 177bbcb..3c37d8c 100644
@@ -104,6 +104,7 @@ static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
 	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
 	if (!vkms_state)
 		return;
+	INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);
 
 	crtc->state = &vkms_state->base;
 	crtc->state->crtc = crtc;
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 8 lines · Tetsuo Handa
Quoted 4 lines · Daniel Jordan

On Sat, Jan 19, 2019 at 02:04:58AM +0900, Tetsuo Handa wrote:
__queue_work has a sanity check already for work, but using list_empty. Seems slightly better to be consistent?

list_empty() won't work, for "struct work_struct" is embedded into a struct which is allocated by kzalloc().

Please check list_empty's definition again, it compares the address of the node to its next pointer, so it should work for a zeroed node. I'll reiterate that it seems slightly better to be consistent in "is work_struct initialized?" checks, but it's not a big deal and I'm fine either way.

Quoted 26 lines

From 1bbf8d9c7aaef78d7f483d2131261162485c6f72 Mon Sep 17 00:00:00 2001
From: Tetsuo Handa <[email protected]>
Date: Sat, 19 Jan 2019 01:38:53 +0900
Subject: [PATCH] drm/vkms: Fix flush_work() without INIT_WORK().

syzbot is hitting a lockdep warning [1] because flush_work() is called without INIT_WORK() after kzalloc() at vkms_atomic_crtc_reset().

Commit 6c234fe37c57627a ("drm/vkms: Implement CRC debugfs API") added INIT_WORK() to only vkms_atomic_crtc_duplicate_state() side. Assuming that lifecycle of crc_work is appropriately managed, fix this problem by adding INIT_WORK() to vkms_atomic_crtc_reset() side.

[1] https://syzkaller.appspot.com/bug?id=a5954455fcfa51c29ca2ab55b203076337e1c770

---
 drivers/gpu/drm/vkms/vkms_crtc.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/vkms/vkms_crtc.c +0 -0
index 177bbcb..3c37d8c 100644

drivers/gpu/drm/vkms/vkms_crtc.c static void vkms_atomic_crtc_reset(struct drm_crtc *crtc) · line 110

Quoted 7 lines
drivers/gpu/drm/vkms/vkms_crtc.c +1 -0
@@ -104,6 +104,7 @@ static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
 	vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
 	if (!vkms_state)
 		return;
+	INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);
 
 	crtc->state = &vkms_state->base;
 	crtc->state->crtc = crtc;

Great, thanks, I think mentioning this path would improve the changelog.

#4 lore raw
Quoted 7 lines · Daniel Jordan
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · Daniel Jordan

On Sat, Jan 19, 2019 at 02:04:58AM +0900, Tetsuo Handa wrote:
__queue_work has a sanity check already for work, but using list_empty. Seems slightly better to be consistent?

Quoted 7 lines
Quoted 2 lines

list_empty() won't work, for "struct work_struct" is embedded into a struct which is allocated by kzalloc().

Please check list_empty's definition again, it compares the address of the node to its next pointer, so it should work for a zeroed node. I'll reiterate that it seems slightly better to be consistent in "is work_struct initialized?" checks, but it's not a big deal and I'm fine either way.

You are talking about

	if (WARN_ON(!list_empty(&work->entry))) {
		spin_unlock(&pwq->pool->lock);
		return;
	}

part in __queue_work(), aren't you? But since flush_work() is used for waiting for a work to complete, that work can be either queued state (list_empty() == false) or not queued state (list_empty() == true). Thus, I don't think that flush_work() can use list_empty() for checking whether that work was initialized.

[PATCH v2] workqueue: Try to catch flush_work() without INIT_WORK().

syzbot found a flush_work() caller who forgot to call INIT_WORK() because that work_struct was allocated by kzalloc() [1]. But the message

INFO: trying to register non-static key.
the code is fine but needs lockdep annotation.
turning off the locking correctness validator.

by lock_map_acquire() is failing to tell that INIT_WORK() is missing.

Since flush_work() without INIT_WORK() is a bug, and INIT_WORK() should set ->func field to non-zero, let's warn if ->func field is zero.

[1] https://syzkaller.appspot.com/bug?id=a5954455fcfa51c29ca2ab55b203076337e1c770

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +3 -0
index 392be4b..a503ad9 100644
@@ -2908,6 +2908,9 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
 	if (WARN_ON(!wq_online))
 		return false;
 
+	if (WARN_ON(!work->func))
+		return false;
+
 	if (!from_cancel) {
 		lock_map_acquire(&work->lockdep_map);
 		lock_map_release(&work->lockdep_map);
Reviewed-by
#5 lore raw
Quoted 23 lines · Tetsuo Handa
Quoted 7 lines · Daniel Jordan
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · Daniel Jordan

On Sat, Jan 19, 2019 at 02:04:58AM +0900, Tetsuo Handa wrote:
__queue_work has a sanity check already for work, but using list_empty. Seems slightly better to be consistent?

Quoted 7 lines
Quoted 2 lines

list_empty() won't work, for "struct work_struct" is embedded into a struct which is allocated by kzalloc().

Please check list_empty's definition again, it compares the address of the node to its next pointer, so it should work for a zeroed node. I'll reiterate that it seems slightly better to be consistent in "is work_struct initialized?" checks, but it's not a big deal and I'm fine either way.

You are talking about

	if (WARN_ON(!list_empty(&work->entry))) {
		spin_unlock(&pwq->pool->lock);
		return;
	}
Quoted 4 lines

part in __queue_work(), aren't you? But since flush_work() is used for waiting for a work to complete, that work can be either queued state (list_empty() == false) or not queued state (list_empty() == true). Thus, I don't think that flush_work() can use list_empty() for checking whether that work was initialized.

Oh, you're right, sorry for the noise!

Quoted 26 lines

[PATCH v2] workqueue: Try to catch flush_work() without INIT_WORK().

syzbot found a flush_work() caller who forgot to call INIT_WORK() because that work_struct was allocated by kzalloc() [1]. But the message

INFO: trying to register non-static key.
the code is fine but needs lockdep annotation.
turning off the locking correctness validator.

by lock_map_acquire() is failing to tell that INIT_WORK() is missing.

Since flush_work() without INIT_WORK() is a bug, and INIT_WORK() should set ->func field to non-zero, let's warn if ->func field is zero.

[1] https://syzkaller.appspot.com/bug?id=a5954455fcfa51c29ca2ab55b203076337e1c770

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +0 -0
index 392be4b..a503ad9 100644

kernel/workqueue.c static bool __flush_work(struct work_struct *work, bool from_cancel) · line 2916

Quoted 9 lines
kernel/workqueue.c +3 -0
@@ -2908,6 +2908,9 @@ static bool __flush_work(struct work_struct *work, bool from_cancel)
 	if (WARN_ON(!wq_online))
 		return false;
 
+	if (WARN_ON(!work->func))
+		return false;
+
 	if (!from_cancel) {
 		lock_map_acquire(&work->lockdep_map);
 		lock_map_release(&work->lockdep_map);

Thanks for updating the changelog. FWIW, you can add

maintainer Applied
#6 lore raw
In reply to Tetsuo Handa
Quoted 39 lines · Tetsuo Handa
Quoted 7 lines · Daniel Jordan
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · Daniel Jordan

On Sat, Jan 19, 2019 at 02:04:58AM +0900, Tetsuo Handa wrote:
__queue_work has a sanity check already for work, but using list_empty. Seems slightly better to be consistent?

Quoted 7 lines
Quoted 2 lines

list_empty() won't work, for "struct work_struct" is embedded into a struct which is allocated by kzalloc().

Please check list_empty's definition again, it compares the address of the node to its next pointer, so it should work for a zeroed node. I'll reiterate that it seems slightly better to be consistent in "is work_struct initialized?" checks, but it's not a big deal and I'm fine either way.

You are talking about

	if (WARN_ON(!list_empty(&work->entry))) {
		spin_unlock(&pwq->pool->lock);
		return;
	}

part in __queue_work(), aren't you? But since flush_work() is used for waiting for a work to complete, that work can be either queued state (list_empty() == false) or not queued state (list_empty() == true). Thus, I don't think that flush_work() can use list_empty() for checking whether that work was initialized.

[PATCH v2] workqueue: Try to catch flush_work() without INIT_WORK().

syzbot found a flush_work() caller who forgot to call INIT_WORK() because that work_struct was allocated by kzalloc() [1]. But the message

INFO: trying to register non-static key.
the code is fine but needs lockdep annotation.
turning off the locking correctness validator.

Quoted 8 lines

by lock_map_acquire() is failing to tell that INIT_WORK() is missing.

Since flush_work() without INIT_WORK() is a bug, and INIT_WORK() should set ->func field to non-zero, let's warn if ->func field is zero.

[1] https://syzkaller.appspot.com/bug?id=a5954455fcfa51c29ca2ab55b203076337e1c770

Applied to wq/for-5.0.

Thanks.

Signature · 1 line
tejun