Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq

Pull workqueue updates from Tejun Heo:

- make kworkers report the workqueue it is executing or has executed
most recently in /proc/PID/comm (so they show up in ps/top)

- CONFIG_SMP shuffle to move stuff which isn't necessary for UP builds
inside CONFIG_SMP.

* 'for-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq:
workqueue: move function definitions within CONFIG_SMP block
workqueue: Make sure struct worker is accessible for wq_worker_comm()
workqueue: Show the latest workqueue name in /proc/PID/{comm,stat,status}
proc: Consolidate task->comm formatting into proc_task_name()
workqueue: Set worker->desc to workqueue name by default
workqueue: Make worker_attach/detach_pool() update worker->pool
workqueue: Replace pool->attach_mutex with global wq_pool_attach_mutex

+119 -64
+20 -13
fs/proc/array.c
··· 96 96 #include <asm/processor.h> 97 97 #include "internal.h" 98 98 99 - static inline void task_name(struct seq_file *m, struct task_struct *p) 99 + void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) 100 100 { 101 101 char *buf; 102 102 size_t size; 103 - char tcomm[sizeof(p->comm)]; 103 + char tcomm[64]; 104 104 int ret; 105 105 106 - get_task_comm(tcomm, p); 107 - 108 - seq_puts(m, "Name:\t"); 106 + if (p->flags & PF_WQ_WORKER) 107 + wq_worker_comm(tcomm, sizeof(tcomm), p); 108 + else 109 + __get_task_comm(tcomm, sizeof(tcomm), p); 109 110 110 111 size = seq_get_buf(m, &buf); 111 - ret = string_escape_str(tcomm, buf, size, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); 112 - seq_commit(m, ret < size ? ret : -1); 112 + if (escape) { 113 + ret = string_escape_str(tcomm, buf, size, 114 + ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); 115 + if (ret >= size) 116 + ret = -1; 117 + } else { 118 + ret = strscpy(buf, tcomm, size); 119 + } 113 120 114 - seq_putc(m, '\n'); 121 + seq_commit(m, ret); 115 122 } 116 123 117 124 /* ··· 397 390 { 398 391 struct mm_struct *mm = get_task_mm(task); 399 392 400 - task_name(m, task); 393 + seq_puts(m, "Name:\t"); 394 + proc_task_name(m, task, true); 395 + seq_putc(m, '\n'); 396 + 401 397 task_state(m, ns, pid, task); 402 398 403 399 if (mm) { ··· 435 425 u64 cutime, cstime, utime, stime; 436 426 u64 cgtime, gtime; 437 427 unsigned long rsslim = 0; 438 - char tcomm[sizeof(task->comm)]; 439 428 unsigned long flags; 440 429 441 430 state = *get_task_state(task); ··· 460 451 } 461 452 } 462 453 } 463 - 464 - get_task_comm(tcomm, task); 465 454 466 455 sigemptyset(&sigign); 467 456 sigemptyset(&sigcatch); ··· 527 520 528 521 seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns)); 529 522 seq_puts(m, " ("); 530 - seq_puts(m, tcomm); 523 + proc_task_name(m, task, false); 531 524 seq_puts(m, ") "); 532 525 seq_putc(m, state); 533 526 seq_put_decimal_ll(m, " ", ppid);
+2 -3
fs/proc/base.c
··· 1563 1563 if (!p) 1564 1564 return -ESRCH; 1565 1565 1566 - task_lock(p); 1567 - seq_printf(m, "%s\n", p->comm); 1568 - task_unlock(p); 1566 + proc_task_name(m, p, false); 1567 + seq_putc(m, '\n'); 1569 1568 1570 1569 put_task_struct(p); 1571 1570
+2
fs/proc/internal.h
··· 136 136 */ 137 137 extern const struct file_operations proc_tid_children_operations; 138 138 139 + extern void proc_task_name(struct seq_file *m, struct task_struct *p, 140 + bool escape); 139 141 extern int proc_tid_stat(struct seq_file *, struct pid_namespace *, 140 142 struct pid *, struct task_struct *); 141 143 extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,
+1
include/linux/workqueue.h
··· 494 494 extern __printf(1, 2) void set_worker_desc(const char *fmt, ...); 495 495 extern void print_worker_info(const char *log_lvl, struct task_struct *task); 496 496 extern void show_workqueue_state(void); 497 + extern void wq_worker_comm(char *buf, size_t size, struct task_struct *task); 497 498 498 499 /** 499 500 * queue_work - queue work on a workqueue
+93 -46
kernel/workqueue.c
··· 66 66 * be executing on any CPU. The pool behaves as an unbound one. 67 67 * 68 68 * Note that DISASSOCIATED should be flipped only while holding 69 - * attach_mutex to avoid changing binding state while 69 + * wq_pool_attach_mutex to avoid changing binding state while 70 70 * worker_attach_to_pool() is in progress. 71 71 */ 72 72 POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ ··· 123 123 * cpu or grabbing pool->lock is enough for read access. If 124 124 * POOL_DISASSOCIATED is set, it's identical to L. 125 125 * 126 - * A: pool->attach_mutex protected. 126 + * A: wq_pool_attach_mutex protected. 127 127 * 128 128 * PL: wq_pool_mutex protected. 129 129 * ··· 166 166 /* L: hash of busy workers */ 167 167 168 168 struct worker *manager; /* L: purely informational */ 169 - struct mutex attach_mutex; /* attach/detach exclusion */ 170 169 struct list_head workers; /* A: attached workers */ 171 170 struct completion *detach_completion; /* all workers detached */ 172 171 ··· 296 297 static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 297 298 298 299 static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 300 + static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 299 301 static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 300 302 static DECLARE_WAIT_QUEUE_HEAD(wq_manager_wait); /* wait for manager to go away */ 301 303 ··· 399 399 * @worker: iteration cursor 400 400 * @pool: worker_pool to iterate workers of 401 401 * 402 - * This must be called with @pool->attach_mutex. 402 + * This must be called with wq_pool_attach_mutex. 403 403 * 404 404 * The if/else clause exists only for the lockdep assertion and can be 405 405 * ignored. 406 406 */ 407 407 #define for_each_pool_worker(worker, pool) \ 408 408 list_for_each_entry((worker), &(pool)->workers, node) \ 409 - if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \ 409 + if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 410 410 else 411 411 412 412 /** ··· 1724 1724 static void worker_attach_to_pool(struct worker *worker, 1725 1725 struct worker_pool *pool) 1726 1726 { 1727 - mutex_lock(&pool->attach_mutex); 1727 + mutex_lock(&wq_pool_attach_mutex); 1728 1728 1729 1729 /* 1730 1730 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any ··· 1733 1733 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 1734 1734 1735 1735 /* 1736 - * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains 1737 - * stable across this function. See the comments above the 1738 - * flag definition for details. 1736 + * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 1737 + * stable across this function. See the comments above the flag 1738 + * definition for details. 1739 1739 */ 1740 1740 if (pool->flags & POOL_DISASSOCIATED) 1741 1741 worker->flags |= WORKER_UNBOUND; 1742 1742 1743 1743 list_add_tail(&worker->node, &pool->workers); 1744 + worker->pool = pool; 1744 1745 1745 - mutex_unlock(&pool->attach_mutex); 1746 + mutex_unlock(&wq_pool_attach_mutex); 1746 1747 } 1747 1748 1748 1749 /** 1749 1750 * worker_detach_from_pool() - detach a worker from its pool 1750 1751 * @worker: worker which is attached to its pool 1751 - * @pool: the pool @worker is attached to 1752 1752 * 1753 1753 * Undo the attaching which had been done in worker_attach_to_pool(). The 1754 1754 * caller worker shouldn't access to the pool after detached except it has 1755 1755 * other reference to the pool. 1756 1756 */ 1757 - static void worker_detach_from_pool(struct worker *worker, 1758 - struct worker_pool *pool) 1757 + static void worker_detach_from_pool(struct worker *worker) 1759 1758 { 1759 + struct worker_pool *pool = worker->pool; 1760 1760 struct completion *detach_completion = NULL; 1761 1761 1762 - mutex_lock(&pool->attach_mutex); 1762 + mutex_lock(&wq_pool_attach_mutex); 1763 + 1763 1764 list_del(&worker->node); 1765 + worker->pool = NULL; 1766 + 1764 1767 if (list_empty(&pool->workers)) 1765 1768 detach_completion = pool->detach_completion; 1766 - mutex_unlock(&pool->attach_mutex); 1769 + mutex_unlock(&wq_pool_attach_mutex); 1767 1770 1768 1771 /* clear leftover flags without pool->lock after it is detached */ 1769 1772 worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); ··· 1802 1799 if (!worker) 1803 1800 goto fail; 1804 1801 1805 - worker->pool = pool; 1806 1802 worker->id = id; 1807 1803 1808 1804 if (pool->cpu >= 0) ··· 2088 2086 worker->current_pwq = pwq; 2089 2087 work_color = get_work_color(work); 2090 2088 2089 + /* 2090 + * Record wq name for cmdline and debug reporting, may get 2091 + * overridden through set_worker_desc(). 2092 + */ 2093 + strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 2094 + 2091 2095 list_del_init(&work->entry); 2092 2096 2093 2097 /* ··· 2189 2181 worker->current_work = NULL; 2190 2182 worker->current_func = NULL; 2191 2183 worker->current_pwq = NULL; 2192 - worker->desc_valid = false; 2193 2184 pwq_dec_nr_in_flight(pwq, work_color); 2194 2185 } 2195 2186 ··· 2213 2206 } 2214 2207 } 2215 2208 2209 + static void set_pf_worker(bool val) 2210 + { 2211 + mutex_lock(&wq_pool_attach_mutex); 2212 + if (val) 2213 + current->flags |= PF_WQ_WORKER; 2214 + else 2215 + current->flags &= ~PF_WQ_WORKER; 2216 + mutex_unlock(&wq_pool_attach_mutex); 2217 + } 2218 + 2216 2219 /** 2217 2220 * worker_thread - the worker thread function 2218 2221 * @__worker: self ··· 2241 2224 struct worker_pool *pool = worker->pool; 2242 2225 2243 2226 /* tell the scheduler that this is a workqueue worker */ 2244 - worker->task->flags |= PF_WQ_WORKER; 2227 + set_pf_worker(true); 2245 2228 woke_up: 2246 2229 spin_lock_irq(&pool->lock); 2247 2230 ··· 2249 2232 if (unlikely(worker->flags & WORKER_DIE)) { 2250 2233 spin_unlock_irq(&pool->lock); 2251 2234 WARN_ON_ONCE(!list_empty(&worker->entry)); 2252 - worker->task->flags &= ~PF_WQ_WORKER; 2235 + set_pf_worker(false); 2253 2236 2254 2237 set_task_comm(worker->task, "kworker/dying"); 2255 2238 ida_simple_remove(&pool->worker_ida, worker->id); 2256 - worker_detach_from_pool(worker, pool); 2239 + worker_detach_from_pool(worker); 2257 2240 kfree(worker); 2258 2241 return 0; 2259 2242 } ··· 2352 2335 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2353 2336 * doesn't participate in concurrency management. 2354 2337 */ 2355 - rescuer->task->flags |= PF_WQ_WORKER; 2338 + set_pf_worker(true); 2356 2339 repeat: 2357 2340 set_current_state(TASK_IDLE); 2358 2341 ··· 2384 2367 worker_attach_to_pool(rescuer, pool); 2385 2368 2386 2369 spin_lock_irq(&pool->lock); 2387 - rescuer->pool = pool; 2388 2370 2389 2371 /* 2390 2372 * Slurp in all works issued via this workqueue and ··· 2433 2417 if (need_more_worker(pool)) 2434 2418 wake_up_worker(pool); 2435 2419 2436 - rescuer->pool = NULL; 2437 2420 spin_unlock_irq(&pool->lock); 2438 2421 2439 - worker_detach_from_pool(rescuer, pool); 2422 + worker_detach_from_pool(rescuer); 2440 2423 2441 2424 spin_lock_irq(&wq_mayday_lock); 2442 2425 } ··· 2444 2429 2445 2430 if (should_stop) { 2446 2431 __set_current_state(TASK_RUNNING); 2447 - rescuer->task->flags &= ~PF_WQ_WORKER; 2432 + set_pf_worker(false); 2448 2433 return 0; 2449 2434 } 2450 2435 ··· 3286 3271 3287 3272 timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 3288 3273 3289 - mutex_init(&pool->attach_mutex); 3290 3274 INIT_LIST_HEAD(&pool->workers); 3291 3275 3292 3276 ida_init(&pool->worker_ida); ··· 3368 3354 WARN_ON(pool->nr_workers || pool->nr_idle); 3369 3355 spin_unlock_irq(&pool->lock); 3370 3356 3371 - mutex_lock(&pool->attach_mutex); 3357 + mutex_lock(&wq_pool_attach_mutex); 3372 3358 if (!list_empty(&pool->workers)) 3373 3359 pool->detach_completion = &detach_completion; 3374 - mutex_unlock(&pool->attach_mutex); 3360 + mutex_unlock(&wq_pool_attach_mutex); 3375 3361 3376 3362 if (pool->detach_completion) 3377 3363 wait_for_completion(pool->detach_completion); ··· 4361 4347 va_start(args, fmt); 4362 4348 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 4363 4349 va_end(args); 4364 - worker->desc_valid = true; 4365 4350 } 4366 4351 } 4367 4352 ··· 4384 4371 char desc[WORKER_DESC_LEN] = { }; 4385 4372 struct pool_workqueue *pwq = NULL; 4386 4373 struct workqueue_struct *wq = NULL; 4387 - bool desc_valid = false; 4388 4374 struct worker *worker; 4389 4375 4390 4376 if (!(task->flags & PF_WQ_WORKER)) ··· 4396 4384 worker = kthread_probe_data(task); 4397 4385 4398 4386 /* 4399 - * Carefully copy the associated workqueue's workfn and name. Keep 4400 - * the original last '\0' in case the original contains garbage. 4387 + * Carefully copy the associated workqueue's workfn, name and desc. 4388 + * Keep the original last '\0' in case the original is garbage. 4401 4389 */ 4402 4390 probe_kernel_read(&fn, &worker->current_func, sizeof(fn)); 4403 4391 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq)); 4404 4392 probe_kernel_read(&wq, &pwq->wq, sizeof(wq)); 4405 4393 probe_kernel_read(name, wq->name, sizeof(name) - 1); 4406 - 4407 - /* copy worker description */ 4408 - probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid)); 4409 - if (desc_valid) 4410 - probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 4394 + probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 4411 4395 4412 4396 if (fn || name[0] || desc[0]) { 4413 4397 printk("%sWorkqueue: %s %pf", log_lvl, name, fn); 4414 - if (desc[0]) 4398 + if (strcmp(name, desc)) 4415 4399 pr_cont(" (%s)", desc); 4416 4400 pr_cont("\n"); 4417 4401 } ··· 4587 4579 rcu_read_unlock_sched(); 4588 4580 } 4589 4581 4582 + /* used to show worker information through /proc/PID/{comm,stat,status} */ 4583 + void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 4584 + { 4585 + int off; 4586 + 4587 + /* always show the actual comm */ 4588 + off = strscpy(buf, task->comm, size); 4589 + if (off < 0) 4590 + return; 4591 + 4592 + /* stabilize PF_WQ_WORKER and worker pool association */ 4593 + mutex_lock(&wq_pool_attach_mutex); 4594 + 4595 + if (task->flags & PF_WQ_WORKER) { 4596 + struct worker *worker = kthread_data(task); 4597 + struct worker_pool *pool = worker->pool; 4598 + 4599 + if (pool) { 4600 + spin_lock_irq(&pool->lock); 4601 + /* 4602 + * ->desc tracks information (wq name or 4603 + * set_worker_desc()) for the latest execution. If 4604 + * current, prepend '+', otherwise '-'. 4605 + */ 4606 + if (worker->desc[0] != '\0') { 4607 + if (worker->current_work) 4608 + scnprintf(buf + off, size - off, "+%s", 4609 + worker->desc); 4610 + else 4611 + scnprintf(buf + off, size - off, "-%s", 4612 + worker->desc); 4613 + } 4614 + spin_unlock_irq(&pool->lock); 4615 + } 4616 + } 4617 + 4618 + mutex_unlock(&wq_pool_attach_mutex); 4619 + } 4620 + 4621 + #ifdef CONFIG_SMP 4622 + 4590 4623 /* 4591 4624 * CPU hotplug. 4592 4625 * ··· 4649 4600 struct worker *worker; 4650 4601 4651 4602 for_each_cpu_worker_pool(pool, cpu) { 4652 - mutex_lock(&pool->attach_mutex); 4603 + mutex_lock(&wq_pool_attach_mutex); 4653 4604 spin_lock_irq(&pool->lock); 4654 4605 4655 4606 /* ··· 4665 4616 pool->flags |= POOL_DISASSOCIATED; 4666 4617 4667 4618 spin_unlock_irq(&pool->lock); 4668 - mutex_unlock(&pool->attach_mutex); 4619 + mutex_unlock(&wq_pool_attach_mutex); 4669 4620 4670 4621 /* 4671 4622 * Call schedule() so that we cross rq->lock and thus can ··· 4706 4657 { 4707 4658 struct worker *worker; 4708 4659 4709 - lockdep_assert_held(&pool->attach_mutex); 4660 + lockdep_assert_held(&wq_pool_attach_mutex); 4710 4661 4711 4662 /* 4712 4663 * Restore CPU affinity of all workers. As all idle workers should ··· 4776 4727 static cpumask_t cpumask; 4777 4728 struct worker *worker; 4778 4729 4779 - lockdep_assert_held(&pool->attach_mutex); 4730 + lockdep_assert_held(&wq_pool_attach_mutex); 4780 4731 4781 4732 /* is @cpu allowed for @pool? */ 4782 4733 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) ··· 4811 4762 mutex_lock(&wq_pool_mutex); 4812 4763 4813 4764 for_each_pool(pool, pi) { 4814 - mutex_lock(&pool->attach_mutex); 4765 + mutex_lock(&wq_pool_attach_mutex); 4815 4766 4816 4767 if (pool->cpu == cpu) 4817 4768 rebind_workers(pool); 4818 4769 else if (pool->cpu < 0) 4819 4770 restore_unbound_workers_cpumask(pool, cpu); 4820 4771 4821 - mutex_unlock(&pool->attach_mutex); 4772 + mutex_unlock(&wq_pool_attach_mutex); 4822 4773 } 4823 4774 4824 4775 /* update NUMA affinity of unbound workqueues */ ··· 4847 4798 4848 4799 return 0; 4849 4800 } 4850 - 4851 - #ifdef CONFIG_SMP 4852 4801 4853 4802 struct work_for_cpu { 4854 4803 struct work_struct work;
+1 -2
kernel/workqueue_internal.h
··· 31 31 struct work_struct *current_work; /* L: work being processed */ 32 32 work_func_t current_func; /* L: current_work's fn */ 33 33 struct pool_workqueue *current_pwq; /* L: current_work's pwq */ 34 - bool desc_valid; /* ->desc is valid */ 35 34 struct list_head scheduled; /* L: scheduled works */ 36 35 37 36 /* 64 bytes boundary on 64bit, 32 on 32bit */ 38 37 39 38 struct task_struct *task; /* I: worker task */ 40 - struct worker_pool *pool; /* I: the associated pool */ 39 + struct worker_pool *pool; /* A: the associated pool */ 41 40 /* L: for rescuers */ 42 41 struct list_head node; /* A: anchored at pool->workers */ 43 42 /* A: runs through worker->node */