Skip to content

Commit 687a9aa

Browse files
committed
workqueue: Make per-cpu pool_workqueues allocated and released like unbound ones
Currently, all per-cpu pwq's (pool_workqueue's) are allocated directly through a per-cpu allocation and thus, unlike unbound workqueues, not reference counted. This difference in lifetime management between the two types is a bit confusing. Unbound workqueues are currently accessed through wq->numa_pwq_tbl[] which isn't suitiable for the planned CPU locality related improvements. The plan is to unify pwq handling across per-cpu and unbound workqueues so that they're always accessed through wq->cpu_pwq. In preparation, this patch makes per-cpu pwq's to be allocated, reference counted and released the same way as unbound pwq's. wq->cpu_pwq now holds pointers to pwq's instead of containing them directly. pwq_unbound_release_workfn() is renamed to pwq_release_workfn() as it's now also used for per-cpu work items. Signed-off-by: Tejun Heo <[email protected]>
1 parent 967b494 commit 687a9aa

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

kernel/workqueue.c

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ struct pool_workqueue {
258258

259259
/*
260260
* Release of unbound pwq is punted to a kthread_worker. See put_pwq()
261-
* and pwq_unbound_release_workfn() for details. pool_workqueue itself
262-
* is also RCU protected so that the first pwq can be determined without
261+
* and pwq_release_workfn() for details. pool_workqueue itself is also
262+
* RCU protected so that the first pwq can be determined without
263263
* grabbing wq->mutex.
264264
*/
265-
struct kthread_work unbound_release_work;
265+
struct kthread_work release_work;
266266
struct rcu_head rcu;
267267
} __aligned(1 << WORK_STRUCT_FLAG_BITS);
268268

@@ -321,7 +321,7 @@ struct workqueue_struct {
321321

322322
/* hot fields used during command issue, aligned to cacheline */
323323
unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
324-
struct pool_workqueue __percpu *cpu_pwq; /* I: per-cpu pwqs */
324+
struct pool_workqueue __percpu **cpu_pwq; /* I: per-cpu pwqs */
325325
struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */
326326
};
327327

@@ -1370,13 +1370,11 @@ static void put_pwq(struct pool_workqueue *pwq)
13701370
lockdep_assert_held(&pwq->pool->lock);
13711371
if (likely(--pwq->refcnt))
13721372
return;
1373-
if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1374-
return;
13751373
/*
13761374
* @pwq can't be released under pool->lock, bounce to a dedicated
13771375
* kthread_worker to avoid A-A deadlocks.
13781376
*/
1379-
kthread_queue_work(pwq_release_worker, &pwq->unbound_release_work);
1377+
kthread_queue_work(pwq_release_worker, &pwq->release_work);
13801378
}
13811379

13821380
/**
@@ -1685,7 +1683,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
16851683
} else {
16861684
if (req_cpu == WORK_CPU_UNBOUND)
16871685
cpu = raw_smp_processor_id();
1688-
pwq = per_cpu_ptr(wq->cpu_pwq, cpu);
1686+
pwq = *per_cpu_ptr(wq->cpu_pwq, cpu);
16891687
}
16901688

16911689
pool = pwq->pool;
@@ -4004,31 +4002,30 @@ static void rcu_free_pwq(struct rcu_head *rcu)
40044002
* Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero
40054003
* refcnt and needs to be destroyed.
40064004
*/
4007-
static void pwq_unbound_release_workfn(struct kthread_work *work)
4005+
static void pwq_release_workfn(struct kthread_work *work)
40084006
{
40094007
struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
4010-
unbound_release_work);
4008+
release_work);
40114009
struct workqueue_struct *wq = pwq->wq;
40124010
struct worker_pool *pool = pwq->pool;
40134011
bool is_last = false;
40144012

40154013
/*
4016-
* when @pwq is not linked, it doesn't hold any reference to the
4014+
* When @pwq is not linked, it doesn't hold any reference to the
40174015
* @wq, and @wq is invalid to access.
40184016
*/
40194017
if (!list_empty(&pwq->pwqs_node)) {
4020-
if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
4021-
return;
4022-
40234018
mutex_lock(&wq->mutex);
40244019
list_del_rcu(&pwq->pwqs_node);
40254020
is_last = list_empty(&wq->pwqs);
40264021
mutex_unlock(&wq->mutex);
40274022
}
40284023

4029-
mutex_lock(&wq_pool_mutex);
4030-
put_unbound_pool(pool);
4031-
mutex_unlock(&wq_pool_mutex);
4024+
if (wq->flags & WQ_UNBOUND) {
4025+
mutex_lock(&wq_pool_mutex);
4026+
put_unbound_pool(pool);
4027+
mutex_unlock(&wq_pool_mutex);
4028+
}
40324029

40334030
call_rcu(&pwq->rcu, rcu_free_pwq);
40344031

@@ -4112,8 +4109,7 @@ static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
41124109
INIT_LIST_HEAD(&pwq->inactive_works);
41134110
INIT_LIST_HEAD(&pwq->pwqs_node);
41144111
INIT_LIST_HEAD(&pwq->mayday_node);
4115-
kthread_init_work(&pwq->unbound_release_work,
4116-
pwq_unbound_release_workfn);
4112+
kthread_init_work(&pwq->release_work, pwq_release_workfn);
41174113
}
41184114

41194115
/* sync @pwq with the current state of its associated wq and link it */
@@ -4514,20 +4510,25 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq)
45144510
int cpu, ret;
45154511

45164512
if (!(wq->flags & WQ_UNBOUND)) {
4517-
wq->cpu_pwq = alloc_percpu(struct pool_workqueue);
4513+
wq->cpu_pwq = alloc_percpu(struct pool_workqueue *);
45184514
if (!wq->cpu_pwq)
4519-
return -ENOMEM;
4515+
goto enomem;
45204516

45214517
for_each_possible_cpu(cpu) {
4522-
struct pool_workqueue *pwq =
4518+
struct pool_workqueue **pwq_p =
45234519
per_cpu_ptr(wq->cpu_pwq, cpu);
4524-
struct worker_pool *cpu_pools =
4525-
per_cpu(cpu_worker_pools, cpu);
4520+
struct worker_pool *pool =
4521+
&(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]);
45264522

4527-
init_pwq(pwq, wq, &cpu_pools[highpri]);
4523+
*pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL,
4524+
pool->node);
4525+
if (!*pwq_p)
4526+
goto enomem;
4527+
4528+
init_pwq(*pwq_p, wq, pool);
45284529

45294530
mutex_lock(&wq->mutex);
4530-
link_pwq(pwq);
4531+
link_pwq(*pwq_p);
45314532
mutex_unlock(&wq->mutex);
45324533
}
45334534
return 0;
@@ -4546,6 +4547,15 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq)
45464547
cpus_read_unlock();
45474548

45484549
return ret;
4550+
4551+
enomem:
4552+
if (wq->cpu_pwq) {
4553+
for_each_possible_cpu(cpu)
4554+
kfree(*per_cpu_ptr(wq->cpu_pwq, cpu));
4555+
free_percpu(wq->cpu_pwq);
4556+
wq->cpu_pwq = NULL;
4557+
}
4558+
return -ENOMEM;
45494559
}
45504560

45514561
static int wq_clamp_max_active(int max_active, unsigned int flags,
@@ -4719,7 +4729,7 @@ static bool pwq_busy(struct pool_workqueue *pwq)
47194729
void destroy_workqueue(struct workqueue_struct *wq)
47204730
{
47214731
struct pool_workqueue *pwq;
4722-
int node;
4732+
int cpu, node;
47234733

47244734
/*
47254735
* Remove it from sysfs first so that sanity check failure doesn't
@@ -4779,12 +4789,8 @@ void destroy_workqueue(struct workqueue_struct *wq)
47794789
mutex_unlock(&wq_pool_mutex);
47804790

47814791
if (!(wq->flags & WQ_UNBOUND)) {
4782-
wq_unregister_lockdep(wq);
4783-
/*
4784-
* The base ref is never dropped on per-cpu pwqs. Directly
4785-
* schedule RCU free.
4786-
*/
4787-
call_rcu(&wq->rcu, rcu_free_wq);
4792+
for_each_possible_cpu(cpu)
4793+
put_pwq_unlocked(*per_cpu_ptr(wq->cpu_pwq, cpu));
47884794
} else {
47894795
/*
47904796
* We're the sole accessor of @wq at this point. Directly
@@ -4901,7 +4907,7 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
49014907
cpu = smp_processor_id();
49024908

49034909
if (!(wq->flags & WQ_UNBOUND))
4904-
pwq = per_cpu_ptr(wq->cpu_pwq, cpu);
4910+
pwq = *per_cpu_ptr(wq->cpu_pwq, cpu);
49054911
else
49064912
pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
49074913

0 commit comments

Comments
 (0)