[RFC PATCH v1 1/6] workqueue: Add set_workqueue_cpumask() helper function

From: Ammar Faizi
Date: Sun Feb 26 2023 - 11:03:44 EST


Allow users to specify a CPU set for the workqueue. The first use case
of this helper function is to set the CPU affinity of Btrfs workqueues.

Signed-off-by: Ammar Faizi <ammarfaizi2@xxxxxxxxxxx>
---
include/linux/workqueue.h | 3 +++
kernel/workqueue.c | 19 +++++++++++++++++++
2 files changed, 22 insertions(+)

diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index ac551b8ee7d9f2f4..e3bd6f47e74ecd66 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -710,6 +710,9 @@ int workqueue_online_cpu(unsigned int cpu);
int workqueue_offline_cpu(unsigned int cpu);
#endif

+int set_workqueue_cpumask(struct workqueue_struct *wq,
+ const cpumask_var_t mask);
+
void __init workqueue_init_early(void);
void __init workqueue_init(void);

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index b8b541caed4854a4..adc1478fafb1811c 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -4398,6 +4398,25 @@ static int init_rescuer(struct workqueue_struct *wq)
return 0;
}

+int set_workqueue_cpumask(struct workqueue_struct *wq, const cpumask_var_t mask)
+{
+ struct workqueue_attrs *tmp_attrs;
+ int ret;
+
+ tmp_attrs = alloc_workqueue_attrs();
+ if (!tmp_attrs)
+ return -ENOMEM;
+
+ apply_wqattrs_lock();
+ copy_workqueue_attrs(tmp_attrs, wq->unbound_attrs);
+ cpumask_copy(tmp_attrs->cpumask, mask);
+ ret = apply_workqueue_attrs_locked(wq, tmp_attrs);
+ apply_wqattrs_unlock();
+ free_workqueue_attrs(tmp_attrs);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(set_workqueue_cpumask);
+
__printf(1, 4)
struct workqueue_struct *alloc_workqueue(const char *fmt,
unsigned int flags,
--
Ammar Faizi