workqueue: Protects wq_unbound_cpumask with wq_pool_attach_mutex

[ Upstream commit 99c621ef24 ]

When unbind_workers() reads wq_unbound_cpumask to set the affinity of
freshly-unbound kworkers, it only holds wq_pool_attach_mutex. This isn't
sufficient as wq_unbound_cpumask is only protected by wq_pool_mutex.

Make wq_unbound_cpumask protected with wq_pool_attach_mutex and also
remove the need of temporary saved_cpumask.

Fixes: 10a5a651e3 ("workqueue: Restrict kworker in the offline CPU pool running on housekeeping CPUs")
Reported-by: Valentin Schneider <vschneid@redhat.com>
Signed-off-by: Lai Jiangshan <jiangshan.ljs@antgroup.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Lai Jiangshan 2023-01-12 16:14:27 +00:00 committed by Greg Kroah-Hartman
parent 807e3596ef
commit 68b2e5d84a
1 changed files with 16 additions and 25 deletions

View File

@ -326,7 +326,7 @@ static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait);
static LIST_HEAD(workqueues); /* PR: list of all workqueues */ static LIST_HEAD(workqueues); /* PR: list of all workqueues */
static bool workqueue_freezing; /* PL: have wqs started freezing? */ static bool workqueue_freezing; /* PL: have wqs started freezing? */
/* PL: allowable cpus for unbound wqs and work items */ /* PL&A: allowable cpus for unbound wqs and work items */
static cpumask_var_t wq_unbound_cpumask; static cpumask_var_t wq_unbound_cpumask;
/* CPU where unbound work was last round robin scheduled from this CPU */ /* CPU where unbound work was last round robin scheduled from this CPU */
@ -3952,7 +3952,8 @@ static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx)
/* allocate the attrs and pwqs for later installation */ /* allocate the attrs and pwqs for later installation */
static struct apply_wqattrs_ctx * static struct apply_wqattrs_ctx *
apply_wqattrs_prepare(struct workqueue_struct *wq, apply_wqattrs_prepare(struct workqueue_struct *wq,
const struct workqueue_attrs *attrs) const struct workqueue_attrs *attrs,
const cpumask_var_t unbound_cpumask)
{ {
struct apply_wqattrs_ctx *ctx; struct apply_wqattrs_ctx *ctx;
struct workqueue_attrs *new_attrs, *tmp_attrs; struct workqueue_attrs *new_attrs, *tmp_attrs;
@ -3968,14 +3969,15 @@ apply_wqattrs_prepare(struct workqueue_struct *wq,
goto out_free; goto out_free;
/* /*
* Calculate the attrs of the default pwq. * Calculate the attrs of the default pwq with unbound_cpumask
* which is wq_unbound_cpumask or to set to wq_unbound_cpumask.
* If the user configured cpumask doesn't overlap with the * If the user configured cpumask doesn't overlap with the
* wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask.
*/ */
copy_workqueue_attrs(new_attrs, attrs); copy_workqueue_attrs(new_attrs, attrs);
cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); cpumask_and(new_attrs->cpumask, new_attrs->cpumask, unbound_cpumask);
if (unlikely(cpumask_empty(new_attrs->cpumask))) if (unlikely(cpumask_empty(new_attrs->cpumask)))
cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); cpumask_copy(new_attrs->cpumask, unbound_cpumask);
/* /*
* We may create multiple pwqs with differing cpumasks. Make a * We may create multiple pwqs with differing cpumasks. Make a
@ -4072,7 +4074,7 @@ static int apply_workqueue_attrs_locked(struct workqueue_struct *wq,
wq->flags &= ~__WQ_ORDERED; wq->flags &= ~__WQ_ORDERED;
} }
ctx = apply_wqattrs_prepare(wq, attrs); ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask);
if (!ctx) if (!ctx)
return -ENOMEM; return -ENOMEM;
@ -5334,7 +5336,7 @@ out_unlock:
} }
#endif /* CONFIG_FREEZER */ #endif /* CONFIG_FREEZER */
static int workqueue_apply_unbound_cpumask(void) static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask)
{ {
LIST_HEAD(ctxs); LIST_HEAD(ctxs);
int ret = 0; int ret = 0;
@ -5350,7 +5352,7 @@ static int workqueue_apply_unbound_cpumask(void)
if (wq->flags & __WQ_ORDERED) if (wq->flags & __WQ_ORDERED)
continue; continue;
ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask);
if (!ctx) { if (!ctx) {
ret = -ENOMEM; ret = -ENOMEM;
break; break;
@ -5365,6 +5367,11 @@ static int workqueue_apply_unbound_cpumask(void)
apply_wqattrs_cleanup(ctx); apply_wqattrs_cleanup(ctx);
} }
if (!ret) {
mutex_lock(&wq_pool_attach_mutex);
cpumask_copy(wq_unbound_cpumask, unbound_cpumask);
mutex_unlock(&wq_pool_attach_mutex);
}
return ret; return ret;
} }
@ -5383,7 +5390,6 @@ static int workqueue_apply_unbound_cpumask(void)
int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
{ {
int ret = -EINVAL; int ret = -EINVAL;
cpumask_var_t saved_cpumask;
/* /*
* Not excluding isolated cpus on purpose. * Not excluding isolated cpus on purpose.
@ -5397,23 +5403,8 @@ int workqueue_set_unbound_cpumask(cpumask_var_t cpumask)
goto out_unlock; goto out_unlock;
} }
if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) { ret = workqueue_apply_unbound_cpumask(cpumask);
ret = -ENOMEM;
goto out_unlock;
}
/* save the old wq_unbound_cpumask. */
cpumask_copy(saved_cpumask, wq_unbound_cpumask);
/* update wq_unbound_cpumask at first and apply it to wqs. */
cpumask_copy(wq_unbound_cpumask, cpumask);
ret = workqueue_apply_unbound_cpumask();
/* restore the wq_unbound_cpumask when failed. */
if (ret < 0)
cpumask_copy(wq_unbound_cpumask, saved_cpumask);
free_cpumask_var(saved_cpumask);
out_unlock: out_unlock:
apply_wqattrs_unlock(); apply_wqattrs_unlock();
} }