Skip to contentSkip to conversation

kernel: Introduce enable_and_queue_work() convenience function

PATCH Superseded Allen Pais 4 participants 9 messages Feb 28, 2024 – Mar 25, 2024 Workqueue

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Allen Pais

author
#0 lore raw

PATCH 1/1 kernel: Introduce enable_and_queue_work() convenience function

The enable_and_queue_work() function is introduced to streamline the process of enabling and queuing a work item on a specific workqueue. This function combines the functionalities of enable_work() and queue_work() in a single call, providing a concise and convenient API for enabling and queuing work items.

The function accepts a target workqueue and a work item as parameters. It first attempts to enable the work item using enable_work(). If the enable operation is successful, the work item is then queued on the specified workqueue using queue_work(). The function returns true if the work item was successfully enabled and queued, and false otherwise.

This addition aims to enhance code readability and maintainability by providing a unified interface for the common use case of enabling and queuing work items on a workqueue.

---
 include/linux/workqueue.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
include/linux/workqueue.h +23 -0
index aedfb81f9c49..31bbd38ef8c8 100644
@@ -678,6 +678,29 @@ static inline bool schedule_work(struct work_struct *work)
 	return queue_work(system_wq, work);
 }
 
+/**
+ * enable_and_queue_work - Enable and queue a work item on a specific workqueue
+ * @wq: The target workqueue
+ * @work: The work item to be enabled and queued
+ *
+ * This function attempts to enable the specified work item using enable_work().
+ * If the enable operation is successful, the work item is then queued on the
+ * provided workqueue using queue_work(). It returns %true if the work item was
+ * successfully enabled and queued, and %false otherwise.
+ *
+ * This function combines the operations of enable_work() and queue_work(),
+ * providing a convenient way to enable and queue a work item in a single call.
+ */
+static inline bool enable_and_queue_work(struct workqueue_struct *wq,
+					 struct work_struct *work)
+{
+	if (enable_work(work)) {
+		queue_work(wq, work);
+		return true;
+	}
+	return false;
+}
+
 /*
  * Detect attempt to flush system-wide workqueues at compile time when possible.
  * Warn attempt to flush system-wide workqueues at runtime.
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 12 lines · Allen Pais

The enable_and_queue_work() function is introduced to streamline the process of enabling and queuing a work item on a specific workqueue. This function combines the functionalities of enable_work() and queue_work() in a single call, providing a concise and convenient API for enabling and queuing work items.

The function accepts a target workqueue and a work item as parameters. It first attempts to enable the work item using enable_work(). If the enable operation is successful, the work item is then queued on the specified workqueue using queue_work(). The function returns true if the work item was successfully enabled and queued, and false otherwise.

Quoted 5 lines

This addition aims to enhance code readability and maintainability by providing a unified interface for the common use case of enabling and queuing work items on a workqueue.

I'll apply this together with the rest of the series once v6.10-rc1 opens.

Thanks.

Signature · 1 line
tejun
author
#2 lore raw
Quoted 13 lines
Quoted 13 lines

The enable_and_queue_work() function is introduced to streamline the process of enabling and queuing a work item on a specific workqueue. This function combines the functionalities of enable_work() and queue_work() in a single call, providing a concise and convenient API for enabling and queuing work items.

The function accepts a target workqueue and a work item as parameters. It first attempts to enable the work item using enable_work(). If the enable operation is successful, the work item is then queued on the specified workqueue using queue_work(). The function returns true if the work item was successfully enabled and queued, and false otherwise.

Quoted 7 lines
Quoted 5 lines

This addition aims to enhance code readability and maintainability by providing a unified interface for the common use case of enabling and queuing work items on a workqueue.

I'll apply this together with the rest of the series once v6.10-rc1 opens.

Thank you. Meanwhile, I will go ahead and prepare the conversion with
Based on this change.

Thanks.

Quoted 5 lines

Thanks.

Signature · 1 line
tejun
reviewer
#7 lore raw
In reply to Allen Pais
Quoted 27 lines · Allen Pais

The enable_and_queue_work() function is introduced to streamline the process of enabling and queuing a work item on a specific workqueue. This function combines the functionalities of enable_work() and queue_work() in a single call, providing a concise and convenient API for enabling and queuing work items.

The function accepts a target workqueue and a work item as parameters. It first attempts to enable the work item using enable_work(). If the enable operation is successful, the work item is then queued on the specified workqueue using queue_work(). The function returns true if the work item was successfully enabled and queued, and false otherwise.

This addition aims to enhance code readability and maintainability by providing a unified interface for the common use case of enabling and queuing work items on a workqueue.

---
 include/linux/workqueue.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
include/linux/workqueue.h +0 -0
index aedfb81f9c49..31bbd38ef8c8 100644

include/linux/workqueue.h static inline bool schedule_work(struct work_struct *work) · line 687

Quoted 10 lines
include/linux/workqueue.h +7 -0
@@ -678,6 +678,29 @@ static inline bool schedule_work(struct work_struct *work)
        return queue_work(system_wq, work);
 }
 
+/**
+ * enable_and_queue_work - Enable and queue a work item on a specific workqueue
+ * @wq: The target workqueue
+ * @work: The work item to be enabled and queued
+ *
+ * This function attempts to enable the specified work item using enable_work().
+ * If the enable operation is successful, the work item is then queued on the

Could you please specify what "successful" means and also please document it that it might cause unnecessary spurious wake-ups and the caller should prepare for it if it is not desired.

Thanks
Lai

PS:

I'm afraid it can cause unnecessary spurious wake-ups in cases where the work item is expected to be dormant ordinarily but disable/enable() are called often for maintenance. However, the user can resort to other synchronization in this case rather than just disable/enable() only to avoid the wake-ups overheads.

Quoted 22 lines
include/linux/workqueue.h +16 -1
+ * provided workqueue using queue_work(). It returns %true if the work item was
+ * successfully enabled and queued, and %false otherwise.
+ *
+ * This function combines the operations of enable_work() and queue_work(),
+ * providing a convenient way to enable and queue a work item in a single call.
+ */
+static inline bool enable_and_queue_work(struct workqueue_struct *wq,
+                                        struct work_struct *work)
+{
+       if (enable_work(work)) {
+               queue_work(wq, work);
+               return true;
+       }
+       return false;
+}
+
 /*
  * Detect attempt to flush system-wide workqueues at compile time when possible.
  * Warn attempt to flush system-wide workqueues at runtime.
--

2.17.1

#8 lore raw
Quoted 9 lines
Quoted 8 lines

+/**
+ * enable_and_queue_work - Enable and queue a work item on a specific workqueue
+ * @wq: The target workqueue
+ * @work: The work item to be enabled and queued
+ *
+ * This function attempts to enable the specified work item using enable_work().
+ * If the enable operation is successful, the work item is then queued on the

Quoted 3 lines

Could you please specify what "successful" means and also please document it that it might cause unnecessary spurious wake-ups and the caller should prepare for it if it is not desired.

Thank you for the review. I will work on documenting it.

Thanks.

Quoted 36 lines

Thanks
Lai

PS:

I'm afraid it can cause unnecessary spurious wake-ups in cases where the work item is expected to be dormant ordinarily but disable/enable() are called often for maintenance. However, the user can resort to other synchronization in this case rather than just disable/enable() only to avoid the wake-ups overheads.

Quoted 22 lines

+ * provided workqueue using queue_work(). It returns %true if the work item was
+ * successfully enabled and queued, and %false otherwise.
+ *
+ * This function combines the operations of enable_work() and queue_work(),
+ * providing a convenient way to enable and queue a work item in a single call.
+ */

include/linux/workqueue.h +10 -1
+static inline bool enable_and_queue_work(struct workqueue_struct *wq,
+                                        struct work_struct *work)
+{
+       if (enable_work(work)) {
+               queue_work(wq, work);
+               return true;
+       }
+       return false;
+}
+
 /*
  * Detect attempt to flush system-wide workqueues at compile time when possible.
  * Warn attempt to flush system-wide workqueues at runtime.
--

2.17.1

Signature · 1 line
       - Allen
#3 lore raw
In reply to Tejun Heo

Tejun

Quoted 6 lines
Quoted 6 lines

The function accepts a target workqueue and a work item as parameters. It first attempts to enable the work item using enable_work(). If the enable operation is successful, the work item is then queued on the specified workqueue using queue_work(). The function returns true if the work item was successfully enabled and queued, and false otherwise.

Quoted 8 lines
Quoted 5 lines

This addition aims to enhance code readability and maintainability by providing a unified interface for the common use case of enabling and queuing work items on a workqueue.

I'll apply this together with the rest of the series once v6.10-rc1 opens.

Could you please let me know once this is applied or point me to the branch.
I have all the conversion based on this.

I have a series ready for review(a set of 10 patches):
https://github.com/allenpais/for-6.9-bh-conversions/tree/for-6.9

A series that still needs some work(testing and cleanup):
https:/github.com/allenpais/for-6.9-bh-conversions/tree/for-6.9-wip (This series contains changes to all the files that use tasklets in drivers/*) I am working on cleaning it up and also testing. They should be ready for review soon.

Thanks.

maintainer
#4 lore raw
Quoted 19 lines · Allen

Tejun

Quoted 6 lines
Quoted 6 lines

The function accepts a target workqueue and a work item as parameters. It first attempts to enable the work item using enable_work(). If the enable operation is successful, the work item is then queued on the specified workqueue using queue_work(). The function returns true if the work item was successfully enabled and queued, and false otherwise.

Quoted 8 lines
Quoted 5 lines

This addition aims to enhance code readability and maintainability by providing a unified interface for the common use case of enabling and queuing work items on a workqueue.

I'll apply this together with the rest of the series once v6.10-rc1 opens.

Could you please let me know once this is applied or point me to the branch.
I have all the conversion based on this.

Quoted 8 lines

I have a series ready for review(a set of 10 patches):
https://github.com/allenpais/for-6.9-bh-conversions/tree/for-6.9

A series that still needs some work(testing and cleanup):
https:/github.com/allenpais/for-6.9-bh-conversions/tree/for-6.9-wip (This series contains changes to all the files that use tasklets in drivers/*) I am working on cleaning it up and also testing. They should be ready for review soon.

Great, I'll open wq/for-6.10 branch once 6.9-rc1 is cut and apply the patches there.

Thanks.

Signature · 1 line
tejun
maintainer
#5 lore raw

Hello,

Quoted 5 lines · Tejun Heo
Quoted 5 lines
Quoted 2 lines

I'll apply this together with the rest of the series once v6.10-rc1 opens.

Could you please let me know once this is applied or point me to the branch.
I have all the conversion based on this.

The enable/disable patchset is applied to wq/for-6.10. I was going to apply the enable_and_queue_work() patch but you wrote that you were gonna update the documentation but I can't find the updated version. Can you please send the updated one?

Thanks.

Signature · 1 line
tejun
#6 lore raw
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Could you please let me know once this is applied or point me to the branch.
I have all the conversion based on this.

The enable/disable patchset is applied to wq/for-6.10. I was going to apply the enable_and_queue_work() patch but you wrote that you were gonna update the documentation but I can't find the updated version. Can you please send the updated one?

Sure, I will send out v2 right away.

Thanks.