Commit a0111cf6 authored by Lai Jiangshan's avatar Lai Jiangshan Committed by Tejun Heo

workqueue: separate out and refactor the locking of applying attrs

Applying attrs requires two locks: get_online_cpus() and wq_pool_mutex,
and this code is duplicated at two places (apply_workqueue_attrs() and
workqueue_set_unbound_cpumask()).  So we separate out this locking
code into apply_wqattrs_[un]lock() and do a minor refactor on
apply_workqueue_attrs().

The apply_wqattrs_[un]lock() will be also used on later patch for
ensuring attrs changes are properly synchronized.

tj: minor updates to comments
Signed-off-by: default avatarLai Jiangshan <laijs@cn.fujitsu.com>
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
parent f7142ed4
...@@ -3621,24 +3621,21 @@ static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) ...@@ -3621,24 +3621,21 @@ static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx)
mutex_unlock(&ctx->wq->mutex); mutex_unlock(&ctx->wq->mutex);
} }
/** static void apply_wqattrs_lock(void)
* apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue {
* @wq: the target workqueue /* CPUs should stay stable across pwq creations and installations */
* @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() get_online_cpus();
* mutex_lock(&wq_pool_mutex);
* Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA }
* machines, this function maps a separate pwq to each NUMA node with
* possibles CPUs in @attrs->cpumask so that work items are affine to the static void apply_wqattrs_unlock(void)
* NUMA node it was issued on. Older pwqs are released as in-flight work {
* items finish. Note that a work item which repeatedly requeues itself mutex_unlock(&wq_pool_mutex);
* back-to-back will stay on its current pwq. put_online_cpus();
* }
* Performs GFP_KERNEL allocations.
* static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
* Return: 0 on success and -errno on failure. const struct workqueue_attrs *attrs)
*/
int apply_workqueue_attrs(struct workqueue_struct *wq,
const struct workqueue_attrs *attrs)
{ {
struct apply_wqattrs_ctx *ctx; struct apply_wqattrs_ctx *ctx;
int ret = -ENOMEM; int ret = -ENOMEM;
...@@ -3651,14 +3648,6 @@ int apply_workqueue_attrs(struct workqueue_struct *wq, ...@@ -3651,14 +3648,6 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,
if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs))) if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
return -EINVAL; return -EINVAL;
/*
* CPUs should stay stable across pwq creations and installations.
* Pin CPUs, determine the target cpumask for each node and create
* pwqs accordingly.
*/
get_online_cpus();
mutex_lock(&wq_pool_mutex);
ctx = apply_wqattrs_prepare(wq, attrs); ctx = apply_wqattrs_prepare(wq, attrs);
/* the ctx has been prepared successfully, let's commit it */ /* the ctx has been prepared successfully, let's commit it */
...@@ -3667,14 +3656,39 @@ int apply_workqueue_attrs(struct workqueue_struct *wq, ...@@ -3667,14 +3656,39 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,
ret = 0; ret = 0;
} }
mutex_unlock(&wq_pool_mutex);
put_online_cpus();
apply_wqattrs_cleanup(ctx); apply_wqattrs_cleanup(ctx);
return ret; return ret;
} }
/**
* apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
* @wq: the target workqueue
* @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
*
* Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
* machines, this function maps a separate pwq to each NUMA node with
* possibles CPUs in @attrs->cpumask so that work items are affine to the
* NUMA node it was issued on. Older pwqs are released as in-flight work
* items finish. Note that a work item which repeatedly requeues itself
* back-to-back will stay on its current pwq.
*
* Performs GFP_KERNEL allocations.
*
* Return: 0 on success and -errno on failure.
*/
int apply_workqueue_attrs(struct workqueue_struct *wq,
const struct workqueue_attrs *attrs)
{
int ret;
apply_wqattrs_lock();
ret = apply_workqueue_attrs_locked(wq, attrs);
apply_wqattrs_unlock();
return ret;
}
/** /**
* wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
* @wq: the target workqueue * @wq: the target workqueue
...@@ -4799,10 +4813,9 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) ...@@ -4799,10 +4813,9 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL))
return -ENOMEM; return -ENOMEM;
get_online_cpus();
cpumask_and(cpumask, cpumask, cpu_possible_mask); cpumask_and(cpumask, cpumask, cpu_possible_mask);
if (!cpumask_empty(cpumask)) { if (!cpumask_empty(cpumask)) {
mutex_lock(&wq_pool_mutex); apply_wqattrs_lock();
/* save the old wq_unbound_cpumask. */ /* save the old wq_unbound_cpumask. */
cpumask_copy(saved_cpumask, wq_unbound_cpumask); cpumask_copy(saved_cpumask, wq_unbound_cpumask);
...@@ -4815,9 +4828,8 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) ...@@ -4815,9 +4828,8 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
if (ret < 0) if (ret < 0)
cpumask_copy(wq_unbound_cpumask, saved_cpumask); cpumask_copy(wq_unbound_cpumask, saved_cpumask);
mutex_unlock(&wq_pool_mutex); apply_wqattrs_unlock();
} }
put_online_cpus();
free_cpumask_var(saved_cpumask); free_cpumask_var(saved_cpumask);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment