Skip to contentSkip to conversation

libfrog: Prevent unnecessary waking of worker thread when using bounded workqueues

PATCH In review Chandan Babu R 3 participants 7 messages Nov 4, 2025 – Nov 6, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libfrog: Prevent unnecessary waking of worker thread when using bounded workqueues

When woken up, a worker will pick a work item from the workqueue and wake up another worker when the current workqueue is a bounded workqueue and if there is atleast one more work item remains to be processed.

The commit 12838bda12e669 ("libfrog: fix overly sleep workqueues") prevented single-threaded processing of work items by waking up sleeping workers when a work item is added to the workqueue. Hence the earlier described mechanism of waking workers is no longer required.

---
 libfrog/workqueue.c | 4 ----
 1 file changed, 4 deletions(-)
libfrog/workqueue.c +0 -4
index db5b3f68b..9384270ff 100644
@@ -51,10 +51,6 @@ workqueue_thread(void *arg)
 		wq->next_item = wi->next;
 		wq->item_count--;
 
-		if (wq->max_queued && wq->next_item) {
-			/* more work, wake up another worker */
-			pthread_cond_signal(&wq->wakeup);
-		}
 		wq->active_threads++;
 		pthread_mutex_unlock(&wq->lock);
 
Signature · 1 line
2.45.2
#1 lore raw

PATCH 2/2 repair/prefetch.c: Create one workqueue with multiple workers

When xfs_repair is executed with a non-zero value for ag_stride, do_inode_prefetch() create multiple workqueues with each of them having just one worker thread.

Since commit 12838bda12e669 ("libfrog: fix overly sleep workqueues"), a workqueue can process multiple work items concurrently. Hence, this commit replaces the above logic with just one workqueue having multiple workers.

---
 repair/prefetch.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
repair/prefetch.c +3 -7
index 5ecf19ae9..8cd3416fa 100644
@@ -1024,7 +1024,6 @@ do_inode_prefetch(
 {
 	int			i;
 	struct workqueue	queue;
-	struct workqueue	*queues;
 	int			queues_started = 0;
 
 	/*
@@ -1056,7 +1055,7 @@ do_inode_prefetch(
 	/*
 	 * create one worker thread for each segment of the volume
 	 */
-	queues = malloc(thread_count * sizeof(struct workqueue));
+	create_work_queue(&queue, mp, thread_count);
 	for (i = 0; i < thread_count; i++) {
 		struct pf_work_args *wargs;
 
@@ -1067,8 +1066,7 @@ do_inode_prefetch(
 		wargs->dirs_only = dirs_only;
 		wargs->func = func;
 
-		create_work_queue(&queues[i], mp, 1);
-		queue_work(&queues[i], prefetch_ag_range_work, 0, wargs);
+		queue_work(&queue, prefetch_ag_range_work, 0, wargs);
 		queues_started++;
 
 		if (wargs->end_ag >= mp->m_sb.sb_agcount)
@@ -1078,9 +1076,7 @@ do_inode_prefetch(
 	/*
 	 * wait for workers to complete
 	 */
-	for (i = 0; i < queues_started; i++)
-		destroy_work_queue(&queues[i]);
-	free(queues);
+	destroy_work_queue(&queue);
 }
 
 void
Signature · 1 line
2.45.2
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] repair/prefetch.c: Create one workqueue with multiple workers

In reply to Chandan Babu R
Quoted 9 lines · Chandan Babu R

When xfs_repair is executed with a non-zero value for ag_stride, do_inode_prefetch() create multiple workqueues with each of them having just one worker thread.

Since commit 12838bda12e669 ("libfrog: fix overly sleep workqueues"), a workqueue can process multiple work items concurrently. Hence, this commit replaces the above logic with just one workqueue having multiple workers.

Yeah, this also makes sense to me. I wonder if this odd code was some sort of workaround for the pre-12838bd workqueue behavior?

--D

Quoted 50 lines
---
 repair/prefetch.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
repair/prefetch.c +3 -7
index 5ecf19ae9..8cd3416fa 100644
@@ -1024,7 +1024,6 @@ do_inode_prefetch(
 {
 	int			i;
 	struct workqueue	queue;
-	struct workqueue	*queues;
 	int			queues_started = 0;
 
 	/*
@@ -1056,7 +1055,7 @@ do_inode_prefetch(
 	/*
 	 * create one worker thread for each segment of the volume
 	 */
-	queues = malloc(thread_count * sizeof(struct workqueue));
+	create_work_queue(&queue, mp, thread_count);
 	for (i = 0; i < thread_count; i++) {
 		struct pf_work_args *wargs;
 
@@ -1067,8 +1066,7 @@ do_inode_prefetch(
 		wargs->dirs_only = dirs_only;
 		wargs->func = func;
 
-		create_work_queue(&queues[i], mp, 1);
-		queue_work(&queues[i], prefetch_ag_range_work, 0, wargs);
+		queue_work(&queue, prefetch_ag_range_work, 0, wargs);
 		queues_started++;
 
 		if (wargs->end_ag >= mp->m_sb.sb_agcount)
@@ -1078,9 +1076,7 @@ do_inode_prefetch(
 	/*
 	 * wait for workers to complete
 	 */
-	for (i = 0; i < queues_started; i++)
-		destroy_work_queue(&queues[i]);
-	free(queues);
+	destroy_work_queue(&queue);
 }
 
 void
Signature · 1 line
2.45.2
#4 lore raw

Re: [PATCH 2/2] repair/prefetch.c: Create one workqueue with multiple workers

Quoted 5 lines · Darrick J. Wong
Quoted 4 lines · Chandan Babu R

When xfs_repair is executed with a non-zero value for ag_stride, do_inode_prefetch() create multiple workqueues with each of them having just one worker thread.

Quoted 9 lines
Quoted 5 lines

Since commit 12838bda12e669 ("libfrog: fix overly sleep workqueues"), a workqueue can process multiple work items concurrently. Hence, this commit replaces the above logic with just one workqueue having multiple workers.

Yeah, this also makes sense to me. I wonder if this odd code was some sort of workaround for the pre-12838bd workqueue behavior?

The old implementation of workqueue which was present in repair/threads.c would wakeup a worker thread only when inserting a new work item to an empty work list. So, yes, this most likely is the reason for creating multiple workqueues.

Signature · 1 line
Chandan
Reviewed-by on v1 1/2
#6 lore raw
In reply to Chandan Babu R
Quoted 10 lines · Chandan Babu R

When woken up, a worker will pick a work item from the workqueue and wake up another worker when the current workqueue is a bounded workqueue and if there is atleast one more work item remains to be processed.

The commit 12838bda12e669 ("libfrog: fix overly sleep workqueues") prevented single-threaded processing of work items by waking up sleeping workers when a work item is added to the workqueue. Hence the earlier described mechanism of waking workers is no longer required.

That makes sense to me, sorry for forgetting to remove this hunk.

--D

Quoted 23 lines
---
 libfrog/workqueue.c | 4 ----
 1 file changed, 4 deletions(-)
libfrog/workqueue.c +0 -4
index db5b3f68b..9384270ff 100644
@@ -51,10 +51,6 @@ workqueue_thread(void *arg)
 		wq->next_item = wi->next;
 		wq->item_count--;
 
-		if (wq->max_queued && wq->next_item) {
-			/* more work, wake up another worker */
-			pthread_cond_signal(&wq->wakeup);
-		}
 		wq->active_threads++;
 		pthread_mutex_unlock(&wq->lock);
 
Signature · 1 line
2.45.2