workqueue.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * linux/kernel/workqueue.c
  3. *
  4. * Generic mechanism for defining kernel helper threads for running
  5. * arbitrary tasks in process context.
  6. *
  7. * Started by Ingo Molnar, Copyright (C) 2002
  8. *
  9. * Derived from the taskqueue/keventd code by:
  10. *
  11. * David Woodhouse <dwmw2@infradead.org>
  12. * Andrew Morton
  13. * Kai Petzke <wpp@marie.physik.tu-berlin.de>
  14. * Theodore Ts'o <tytso@mit.edu>
  15. *
  16. * Made to use alloc_percpu by Christoph Lameter.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/init.h>
  22. #include <linux/signal.h>
  23. #include <linux/completion.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/slab.h>
  26. #include <linux/cpu.h>
  27. #include <linux/notifier.h>
  28. #include <linux/kthread.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/mempolicy.h>
  31. #include <linux/freezer.h>
  32. #include <linux/kallsyms.h>
  33. #include <linux/debug_locks.h>
  34. #include <linux/lockdep.h>
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/workqueue.h>
  37. /*
  38. * The per-CPU workqueue (if single thread, we always use the first
  39. * possible cpu).
  40. */
  41. struct cpu_workqueue_struct {
  42. spinlock_t lock;
  43. struct list_head worklist;
  44. wait_queue_head_t more_work;
  45. struct work_struct *current_work;
  46. struct workqueue_struct *wq;
  47. struct task_struct *thread;
  48. } ____cacheline_aligned;
  49. /*
  50. * The externally visible workqueue abstraction is an array of
  51. * per-CPU workqueues:
  52. */
  53. struct workqueue_struct {
  54. struct cpu_workqueue_struct *cpu_wq;
  55. struct list_head list;
  56. const char *name;
  57. int singlethread;
  58. int freezeable; /* Freeze threads during suspend */
  59. int rt;
  60. #ifdef CONFIG_LOCKDEP
  61. struct lockdep_map lockdep_map;
  62. #endif
  63. };
  64. /* Serializes the accesses to the list of workqueues. */
  65. static DEFINE_SPINLOCK(workqueue_lock);
  66. static LIST_HEAD(workqueues);
  67. static int singlethread_cpu __read_mostly;
  68. static const struct cpumask *cpu_singlethread_map __read_mostly;
  69. /*
  70. * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
  71. * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
  72. * which comes in between can't use for_each_online_cpu(). We could
  73. * use cpu_possible_map, the cpumask below is more a documentation
  74. * than optimization.
  75. */
  76. static cpumask_var_t cpu_populated_map __read_mostly;
  77. /* If it's single threaded, it isn't in the list of workqueues. */
  78. static inline int is_wq_single_threaded(struct workqueue_struct *wq)
  79. {
  80. return wq->singlethread;
  81. }
  82. static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq)
  83. {
  84. return is_wq_single_threaded(wq)
  85. ? cpu_singlethread_map : cpu_populated_map;
  86. }
  87. static
  88. struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
  89. {
  90. if (unlikely(is_wq_single_threaded(wq)))
  91. cpu = singlethread_cpu;
  92. return per_cpu_ptr(wq->cpu_wq, cpu);
  93. }
  94. /*
  95. * Set the workqueue on which a work item is to be run
  96. * - Must *only* be called if the pending flag is set
  97. */
  98. static inline void set_wq_data(struct work_struct *work,
  99. struct cpu_workqueue_struct *cwq)
  100. {
  101. unsigned long new;
  102. BUG_ON(!work_pending(work));
  103. new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
  104. new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
  105. atomic_long_set(&work->data, new);
  106. }
  107. static inline
  108. struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  109. {
  110. return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
  111. }
  112. static void insert_work(struct cpu_workqueue_struct *cwq,
  113. struct work_struct *work, struct list_head *head)
  114. {
  115. trace_workqueue_insertion(cwq->thread, work);
  116. set_wq_data(work, cwq);
  117. /*
  118. * Ensure that we get the right work->data if we see the
  119. * result of list_add() below, see try_to_grab_pending().
  120. */
  121. smp_wmb();
  122. list_add_tail(&work->entry, head);
  123. wake_up(&cwq->more_work);
  124. }
  125. static void __queue_work(struct cpu_workqueue_struct *cwq,
  126. struct work_struct *work)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&cwq->lock, flags);
  130. insert_work(cwq, work, &cwq->worklist);
  131. spin_unlock_irqrestore(&cwq->lock, flags);
  132. }
  133. /**
  134. * queue_work - queue work on a workqueue
  135. * @wq: workqueue to use
  136. * @work: work to queue
  137. *
  138. * Returns 0 if @work was already on a queue, non-zero otherwise.
  139. *
  140. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  141. * it can be processed by another CPU.
  142. */
  143. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  144. {
  145. int ret;
  146. ret = queue_work_on(get_cpu(), wq, work);
  147. put_cpu();
  148. return ret;
  149. }
  150. EXPORT_SYMBOL_GPL(queue_work);
  151. /**
  152. * queue_work_on - queue work on specific cpu
  153. * @cpu: CPU number to execute work on
  154. * @wq: workqueue to use
  155. * @work: work to queue
  156. *
  157. * Returns 0 if @work was already on a queue, non-zero otherwise.
  158. *
  159. * We queue the work to a specific CPU, the caller must ensure it
  160. * can't go away.
  161. */
  162. int
  163. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  164. {
  165. int ret = 0;
  166. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  167. BUG_ON(!list_empty(&work->entry));
  168. __queue_work(wq_per_cpu(wq, cpu), work);
  169. ret = 1;
  170. }
  171. return ret;
  172. }
  173. EXPORT_SYMBOL_GPL(queue_work_on);
  174. static void delayed_work_timer_fn(unsigned long __data)
  175. {
  176. struct delayed_work *dwork = (struct delayed_work *)__data;
  177. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  178. struct workqueue_struct *wq = cwq->wq;
  179. __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
  180. }
  181. /**
  182. * queue_delayed_work - queue work on a workqueue after delay
  183. * @wq: workqueue to use
  184. * @dwork: delayable work to queue
  185. * @delay: number of jiffies to wait before queueing
  186. *
  187. * Returns 0 if @work was already on a queue, non-zero otherwise.
  188. */
  189. int queue_delayed_work(struct workqueue_struct *wq,
  190. struct delayed_work *dwork, unsigned long delay)
  191. {
  192. if (delay == 0)
  193. return queue_work(wq, &dwork->work);
  194. return queue_delayed_work_on(-1, wq, dwork, delay);
  195. }
  196. EXPORT_SYMBOL_GPL(queue_delayed_work);
  197. /**
  198. * queue_delayed_work_on - queue work on specific CPU after delay
  199. * @cpu: CPU number to execute work on
  200. * @wq: workqueue to use
  201. * @dwork: work to queue
  202. * @delay: number of jiffies to wait before queueing
  203. *
  204. * Returns 0 if @work was already on a queue, non-zero otherwise.
  205. */
  206. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  207. struct delayed_work *dwork, unsigned long delay)
  208. {
  209. int ret = 0;
  210. struct timer_list *timer = &dwork->timer;
  211. struct work_struct *work = &dwork->work;
  212. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
  213. BUG_ON(timer_pending(timer));
  214. BUG_ON(!list_empty(&work->entry));
  215. timer_stats_timer_set_start_info(&dwork->timer);
  216. /* This stores cwq for the moment, for the timer_fn */
  217. set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
  218. timer->expires = jiffies + delay;
  219. timer->data = (unsigned long)dwork;
  220. timer->function = delayed_work_timer_fn;
  221. if (unlikely(cpu >= 0))
  222. add_timer_on(timer, cpu);
  223. else
  224. add_timer(timer);
  225. ret = 1;
  226. }
  227. return ret;
  228. }
  229. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  230. static void run_workqueue(struct cpu_workqueue_struct *cwq)
  231. {
  232. spin_lock_irq(&cwq->lock);
  233. while (!list_empty(&cwq->worklist)) {
  234. struct work_struct *work = list_entry(cwq->worklist.next,
  235. struct work_struct, entry);
  236. work_func_t f = work->func;
  237. #ifdef CONFIG_LOCKDEP
  238. /*
  239. * It is permissible to free the struct work_struct
  240. * from inside the function that is called from it,
  241. * this we need to take into account for lockdep too.
  242. * To avoid bogus "held lock freed" warnings as well
  243. * as problems when looking into work->lockdep_map,
  244. * make a copy and use that here.
  245. */
  246. struct lockdep_map lockdep_map = work->lockdep_map;
  247. #endif
  248. trace_workqueue_execution(cwq->thread, work);
  249. cwq->current_work = work;
  250. list_del_init(cwq->worklist.next);
  251. spin_unlock_irq(&cwq->lock);
  252. BUG_ON(get_wq_data(work) != cwq);
  253. work_clear_pending(work);
  254. lock_map_acquire(&cwq->wq->lockdep_map);
  255. lock_map_acquire(&lockdep_map);
  256. f(work);
  257. lock_map_release(&lockdep_map);
  258. lock_map_release(&cwq->wq->lockdep_map);
  259. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  260. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  261. "%s/0x%08x/%d\n",
  262. current->comm, preempt_count(),
  263. task_pid_nr(current));
  264. printk(KERN_ERR " last function: ");
  265. print_symbol("%s\n", (unsigned long)f);
  266. debug_show_held_locks(current);
  267. dump_stack();
  268. }
  269. spin_lock_irq(&cwq->lock);
  270. cwq->current_work = NULL;
  271. }
  272. spin_unlock_irq(&cwq->lock);
  273. }
  274. static int worker_thread(void *__cwq)
  275. {
  276. struct cpu_workqueue_struct *cwq = __cwq;
  277. DEFINE_WAIT(wait);
  278. if (cwq->wq->freezeable)
  279. set_freezable();
  280. for (;;) {
  281. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  282. if (!freezing(current) &&
  283. !kthread_should_stop() &&
  284. list_empty(&cwq->worklist))
  285. schedule();
  286. finish_wait(&cwq->more_work, &wait);
  287. try_to_freeze();
  288. if (kthread_should_stop())
  289. break;
  290. run_workqueue(cwq);
  291. }
  292. return 0;
  293. }
  294. struct wq_barrier {
  295. struct work_struct work;
  296. struct completion done;
  297. };
  298. static void wq_barrier_func(struct work_struct *work)
  299. {
  300. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  301. complete(&barr->done);
  302. }
  303. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  304. struct wq_barrier *barr, struct list_head *head)
  305. {
  306. INIT_WORK(&barr->work, wq_barrier_func);
  307. __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
  308. init_completion(&barr->done);
  309. insert_work(cwq, &barr->work, head);
  310. }
  311. static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
  312. {
  313. int active = 0;
  314. struct wq_barrier barr;
  315. WARN_ON(cwq->thread == current);
  316. spin_lock_irq(&cwq->lock);
  317. if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
  318. insert_wq_barrier(cwq, &barr, &cwq->worklist);
  319. active = 1;
  320. }
  321. spin_unlock_irq(&cwq->lock);
  322. if (active)
  323. wait_for_completion(&barr.done);
  324. return active;
  325. }
  326. /**
  327. * flush_workqueue - ensure that any scheduled work has run to completion.
  328. * @wq: workqueue to flush
  329. *
  330. * Forces execution of the workqueue and blocks until its completion.
  331. * This is typically used in driver shutdown handlers.
  332. *
  333. * We sleep until all works which were queued on entry have been handled,
  334. * but we are not livelocked by new incoming ones.
  335. *
  336. * This function used to run the workqueues itself. Now we just wait for the
  337. * helper threads to do it.
  338. */
  339. void flush_workqueue(struct workqueue_struct *wq)
  340. {
  341. const struct cpumask *cpu_map = wq_cpu_map(wq);
  342. int cpu;
  343. might_sleep();
  344. lock_map_acquire(&wq->lockdep_map);
  345. lock_map_release(&wq->lockdep_map);
  346. for_each_cpu(cpu, cpu_map)
  347. flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
  348. }
  349. EXPORT_SYMBOL_GPL(flush_workqueue);
  350. /**
  351. * flush_work - block until a work_struct's callback has terminated
  352. * @work: the work which is to be flushed
  353. *
  354. * Returns false if @work has already terminated.
  355. *
  356. * It is expected that, prior to calling flush_work(), the caller has
  357. * arranged for the work to not be requeued, otherwise it doesn't make
  358. * sense to use this function.
  359. */
  360. int flush_work(struct work_struct *work)
  361. {
  362. struct cpu_workqueue_struct *cwq;
  363. struct list_head *prev;
  364. struct wq_barrier barr;
  365. might_sleep();
  366. cwq = get_wq_data(work);
  367. if (!cwq)
  368. return 0;
  369. lock_map_acquire(&cwq->wq->lockdep_map);
  370. lock_map_release(&cwq->wq->lockdep_map);
  371. prev = NULL;
  372. spin_lock_irq(&cwq->lock);
  373. if (!list_empty(&work->entry)) {
  374. /*
  375. * See the comment near try_to_grab_pending()->smp_rmb().
  376. * If it was re-queued under us we are not going to wait.
  377. */
  378. smp_rmb();
  379. if (unlikely(cwq != get_wq_data(work)))
  380. goto out;
  381. prev = &work->entry;
  382. } else {
  383. if (cwq->current_work != work)
  384. goto out;
  385. prev = &cwq->worklist;
  386. }
  387. insert_wq_barrier(cwq, &barr, prev->next);
  388. out:
  389. spin_unlock_irq(&cwq->lock);
  390. if (!prev)
  391. return 0;
  392. wait_for_completion(&barr.done);
  393. return 1;
  394. }
  395. EXPORT_SYMBOL_GPL(flush_work);
  396. /*
  397. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  398. * so this work can't be re-armed in any way.
  399. */
  400. static int try_to_grab_pending(struct work_struct *work)
  401. {
  402. struct cpu_workqueue_struct *cwq;
  403. int ret = -1;
  404. if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
  405. return 0;
  406. /*
  407. * The queueing is in progress, or it is already queued. Try to
  408. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  409. */
  410. cwq = get_wq_data(work);
  411. if (!cwq)
  412. return ret;
  413. spin_lock_irq(&cwq->lock);
  414. if (!list_empty(&work->entry)) {
  415. /*
  416. * This work is queued, but perhaps we locked the wrong cwq.
  417. * In that case we must see the new value after rmb(), see
  418. * insert_work()->wmb().
  419. */
  420. smp_rmb();
  421. if (cwq == get_wq_data(work)) {
  422. list_del_init(&work->entry);
  423. ret = 1;
  424. }
  425. }
  426. spin_unlock_irq(&cwq->lock);
  427. return ret;
  428. }
  429. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  430. struct work_struct *work)
  431. {
  432. struct wq_barrier barr;
  433. int running = 0;
  434. spin_lock_irq(&cwq->lock);
  435. if (unlikely(cwq->current_work == work)) {
  436. insert_wq_barrier(cwq, &barr, cwq->worklist.next);
  437. running = 1;
  438. }
  439. spin_unlock_irq(&cwq->lock);
  440. if (unlikely(running))
  441. wait_for_completion(&barr.done);
  442. }
  443. static void wait_on_work(struct work_struct *work)
  444. {
  445. struct cpu_workqueue_struct *cwq;
  446. struct workqueue_struct *wq;
  447. const struct cpumask *cpu_map;
  448. int cpu;
  449. might_sleep();
  450. lock_map_acquire(&work->lockdep_map);
  451. lock_map_release(&work->lockdep_map);
  452. cwq = get_wq_data(work);
  453. if (!cwq)
  454. return;
  455. wq = cwq->wq;
  456. cpu_map = wq_cpu_map(wq);
  457. for_each_cpu(cpu, cpu_map)
  458. wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
  459. }
  460. static int __cancel_work_timer(struct work_struct *work,
  461. struct timer_list* timer)
  462. {
  463. int ret;
  464. do {
  465. ret = (timer && likely(del_timer(timer)));
  466. if (!ret)
  467. ret = try_to_grab_pending(work);
  468. wait_on_work(work);
  469. } while (unlikely(ret < 0));
  470. work_clear_pending(work);
  471. return ret;
  472. }
  473. /**
  474. * cancel_work_sync - block until a work_struct's callback has terminated
  475. * @work: the work which is to be flushed
  476. *
  477. * Returns true if @work was pending.
  478. *
  479. * cancel_work_sync() will cancel the work if it is queued. If the work's
  480. * callback appears to be running, cancel_work_sync() will block until it
  481. * has completed.
  482. *
  483. * It is possible to use this function if the work re-queues itself. It can
  484. * cancel the work even if it migrates to another workqueue, however in that
  485. * case it only guarantees that work->func() has completed on the last queued
  486. * workqueue.
  487. *
  488. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  489. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  490. *
  491. * The caller must ensure that workqueue_struct on which this work was last
  492. * queued can't be destroyed before this function returns.
  493. */
  494. int cancel_work_sync(struct work_struct *work)
  495. {
  496. return __cancel_work_timer(work, NULL);
  497. }
  498. EXPORT_SYMBOL_GPL(cancel_work_sync);
  499. /**
  500. * cancel_delayed_work_sync - reliably kill off a delayed work.
  501. * @dwork: the delayed work struct
  502. *
  503. * Returns true if @dwork was pending.
  504. *
  505. * It is possible to use this function if @dwork rearms itself via queue_work()
  506. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  507. */
  508. int cancel_delayed_work_sync(struct delayed_work *dwork)
  509. {
  510. return __cancel_work_timer(&dwork->work, &dwork->timer);
  511. }
  512. EXPORT_SYMBOL(cancel_delayed_work_sync);
  513. static struct workqueue_struct *keventd_wq __read_mostly;
  514. /**
  515. * schedule_work - put work task in global workqueue
  516. * @work: job to be done
  517. *
  518. * This puts a job in the kernel-global workqueue.
  519. */
  520. int schedule_work(struct work_struct *work)
  521. {
  522. return queue_work(keventd_wq, work);
  523. }
  524. EXPORT_SYMBOL(schedule_work);
  525. /*
  526. * schedule_work_on - put work task on a specific cpu
  527. * @cpu: cpu to put the work task on
  528. * @work: job to be done
  529. *
  530. * This puts a job on a specific cpu
  531. */
  532. int schedule_work_on(int cpu, struct work_struct *work)
  533. {
  534. return queue_work_on(cpu, keventd_wq, work);
  535. }
  536. EXPORT_SYMBOL(schedule_work_on);
  537. /**
  538. * schedule_delayed_work - put work task in global workqueue after delay
  539. * @dwork: job to be done
  540. * @delay: number of jiffies to wait or 0 for immediate execution
  541. *
  542. * After waiting for a given time this puts a job in the kernel-global
  543. * workqueue.
  544. */
  545. int schedule_delayed_work(struct delayed_work *dwork,
  546. unsigned long delay)
  547. {
  548. return queue_delayed_work(keventd_wq, dwork, delay);
  549. }
  550. EXPORT_SYMBOL(schedule_delayed_work);
  551. /**
  552. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  553. * @cpu: cpu to use
  554. * @dwork: job to be done
  555. * @delay: number of jiffies to wait
  556. *
  557. * After waiting for a given time this puts a job in the kernel-global
  558. * workqueue on the specified CPU.
  559. */
  560. int schedule_delayed_work_on(int cpu,
  561. struct delayed_work *dwork, unsigned long delay)
  562. {
  563. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  564. }
  565. EXPORT_SYMBOL(schedule_delayed_work_on);
  566. /**
  567. * schedule_on_each_cpu - call a function on each online CPU from keventd
  568. * @func: the function to call
  569. *
  570. * Returns zero on success.
  571. * Returns -ve errno on failure.
  572. *
  573. * schedule_on_each_cpu() is very slow.
  574. */
  575. int schedule_on_each_cpu(work_func_t func)
  576. {
  577. int cpu;
  578. struct work_struct *works;
  579. works = alloc_percpu(struct work_struct);
  580. if (!works)
  581. return -ENOMEM;
  582. get_online_cpus();
  583. for_each_online_cpu(cpu) {
  584. struct work_struct *work = per_cpu_ptr(works, cpu);
  585. INIT_WORK(work, func);
  586. schedule_work_on(cpu, work);
  587. }
  588. for_each_online_cpu(cpu)
  589. flush_work(per_cpu_ptr(works, cpu));
  590. put_online_cpus();
  591. free_percpu(works);
  592. return 0;
  593. }
  594. void flush_scheduled_work(void)
  595. {
  596. flush_workqueue(keventd_wq);
  597. }
  598. EXPORT_SYMBOL(flush_scheduled_work);
  599. /**
  600. * execute_in_process_context - reliably execute the routine with user context
  601. * @fn: the function to execute
  602. * @ew: guaranteed storage for the execute work structure (must
  603. * be available when the work executes)
  604. *
  605. * Executes the function immediately if process context is available,
  606. * otherwise schedules the function for delayed execution.
  607. *
  608. * Returns: 0 - function was executed
  609. * 1 - function was scheduled for execution
  610. */
  611. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  612. {
  613. if (!in_interrupt()) {
  614. fn(&ew->work);
  615. return 0;
  616. }
  617. INIT_WORK(&ew->work, fn);
  618. schedule_work(&ew->work);
  619. return 1;
  620. }
  621. EXPORT_SYMBOL_GPL(execute_in_process_context);
  622. int keventd_up(void)
  623. {
  624. return keventd_wq != NULL;
  625. }
  626. int current_is_keventd(void)
  627. {
  628. struct cpu_workqueue_struct *cwq;
  629. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  630. int ret = 0;
  631. BUG_ON(!keventd_wq);
  632. cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
  633. if (current == cwq->thread)
  634. ret = 1;
  635. return ret;
  636. }
  637. static struct cpu_workqueue_struct *
  638. init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
  639. {
  640. struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  641. cwq->wq = wq;
  642. spin_lock_init(&cwq->lock);
  643. INIT_LIST_HEAD(&cwq->worklist);
  644. init_waitqueue_head(&cwq->more_work);
  645. return cwq;
  646. }
  647. static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  648. {
  649. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  650. struct workqueue_struct *wq = cwq->wq;
  651. const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d";
  652. struct task_struct *p;
  653. p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
  654. /*
  655. * Nobody can add the work_struct to this cwq,
  656. * if (caller is __create_workqueue)
  657. * nobody should see this wq
  658. * else // caller is CPU_UP_PREPARE
  659. * cpu is not on cpu_online_map
  660. * so we can abort safely.
  661. */
  662. if (IS_ERR(p))
  663. return PTR_ERR(p);
  664. if (cwq->wq->rt)
  665. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  666. cwq->thread = p;
  667. trace_workqueue_creation(cwq->thread, cpu);
  668. return 0;
  669. }
  670. static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
  671. {
  672. struct task_struct *p = cwq->thread;
  673. if (p != NULL) {
  674. if (cpu >= 0)
  675. kthread_bind(p, cpu);
  676. wake_up_process(p);
  677. }
  678. }
  679. struct workqueue_struct *__create_workqueue_key(const char *name,
  680. int singlethread,
  681. int freezeable,
  682. int rt,
  683. struct lock_class_key *key,
  684. const char *lock_name)
  685. {
  686. struct workqueue_struct *wq;
  687. struct cpu_workqueue_struct *cwq;
  688. int err = 0, cpu;
  689. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  690. if (!wq)
  691. return NULL;
  692. wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
  693. if (!wq->cpu_wq) {
  694. kfree(wq);
  695. return NULL;
  696. }
  697. wq->name = name;
  698. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  699. wq->singlethread = singlethread;
  700. wq->freezeable = freezeable;
  701. wq->rt = rt;
  702. INIT_LIST_HEAD(&wq->list);
  703. if (singlethread) {
  704. cwq = init_cpu_workqueue(wq, singlethread_cpu);
  705. err = create_workqueue_thread(cwq, singlethread_cpu);
  706. start_workqueue_thread(cwq, -1);
  707. } else {
  708. cpu_maps_update_begin();
  709. /*
  710. * We must place this wq on list even if the code below fails.
  711. * cpu_down(cpu) can remove cpu from cpu_populated_map before
  712. * destroy_workqueue() takes the lock, in that case we leak
  713. * cwq[cpu]->thread.
  714. */
  715. spin_lock(&workqueue_lock);
  716. list_add(&wq->list, &workqueues);
  717. spin_unlock(&workqueue_lock);
  718. /*
  719. * We must initialize cwqs for each possible cpu even if we
  720. * are going to call destroy_workqueue() finally. Otherwise
  721. * cpu_up() can hit the uninitialized cwq once we drop the
  722. * lock.
  723. */
  724. for_each_possible_cpu(cpu) {
  725. cwq = init_cpu_workqueue(wq, cpu);
  726. if (err || !cpu_online(cpu))
  727. continue;
  728. err = create_workqueue_thread(cwq, cpu);
  729. start_workqueue_thread(cwq, cpu);
  730. }
  731. cpu_maps_update_done();
  732. }
  733. if (err) {
  734. destroy_workqueue(wq);
  735. wq = NULL;
  736. }
  737. return wq;
  738. }
  739. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  740. static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
  741. {
  742. /*
  743. * Our caller is either destroy_workqueue() or CPU_POST_DEAD,
  744. * cpu_add_remove_lock protects cwq->thread.
  745. */
  746. if (cwq->thread == NULL)
  747. return;
  748. lock_map_acquire(&cwq->wq->lockdep_map);
  749. lock_map_release(&cwq->wq->lockdep_map);
  750. flush_cpu_workqueue(cwq);
  751. /*
  752. * If the caller is CPU_POST_DEAD and cwq->worklist was not empty,
  753. * a concurrent flush_workqueue() can insert a barrier after us.
  754. * However, in that case run_workqueue() won't return and check
  755. * kthread_should_stop() until it flushes all work_struct's.
  756. * When ->worklist becomes empty it is safe to exit because no
  757. * more work_structs can be queued on this cwq: flush_workqueue
  758. * checks list_empty(), and a "normal" queue_work() can't use
  759. * a dead CPU.
  760. */
  761. trace_workqueue_destruction(cwq->thread);
  762. kthread_stop(cwq->thread);
  763. cwq->thread = NULL;
  764. }
  765. /**
  766. * destroy_workqueue - safely terminate a workqueue
  767. * @wq: target workqueue
  768. *
  769. * Safely destroy a workqueue. All work currently pending will be done first.
  770. */
  771. void destroy_workqueue(struct workqueue_struct *wq)
  772. {
  773. const struct cpumask *cpu_map = wq_cpu_map(wq);
  774. int cpu;
  775. cpu_maps_update_begin();
  776. spin_lock(&workqueue_lock);
  777. list_del(&wq->list);
  778. spin_unlock(&workqueue_lock);
  779. for_each_cpu(cpu, cpu_map)
  780. cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
  781. cpu_maps_update_done();
  782. free_percpu(wq->cpu_wq);
  783. kfree(wq);
  784. }
  785. EXPORT_SYMBOL_GPL(destroy_workqueue);
  786. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  787. unsigned long action,
  788. void *hcpu)
  789. {
  790. unsigned int cpu = (unsigned long)hcpu;
  791. struct cpu_workqueue_struct *cwq;
  792. struct workqueue_struct *wq;
  793. int ret = NOTIFY_OK;
  794. action &= ~CPU_TASKS_FROZEN;
  795. switch (action) {
  796. case CPU_UP_PREPARE:
  797. cpumask_set_cpu(cpu, cpu_populated_map);
  798. }
  799. undo:
  800. list_for_each_entry(wq, &workqueues, list) {
  801. cwq = per_cpu_ptr(wq->cpu_wq, cpu);
  802. switch (action) {
  803. case CPU_UP_PREPARE:
  804. if (!create_workqueue_thread(cwq, cpu))
  805. break;
  806. printk(KERN_ERR "workqueue [%s] for %i failed\n",
  807. wq->name, cpu);
  808. action = CPU_UP_CANCELED;
  809. ret = NOTIFY_BAD;
  810. goto undo;
  811. case CPU_ONLINE:
  812. start_workqueue_thread(cwq, cpu);
  813. break;
  814. case CPU_UP_CANCELED:
  815. start_workqueue_thread(cwq, -1);
  816. case CPU_POST_DEAD:
  817. cleanup_workqueue_thread(cwq);
  818. break;
  819. }
  820. }
  821. switch (action) {
  822. case CPU_UP_CANCELED:
  823. case CPU_POST_DEAD:
  824. cpumask_clear_cpu(cpu, cpu_populated_map);
  825. }
  826. return ret;
  827. }
  828. #ifdef CONFIG_SMP
  829. struct work_for_cpu {
  830. struct completion completion;
  831. long (*fn)(void *);
  832. void *arg;
  833. long ret;
  834. };
  835. static int do_work_for_cpu(void *_wfc)
  836. {
  837. struct work_for_cpu *wfc = _wfc;
  838. wfc->ret = wfc->fn(wfc->arg);
  839. complete(&wfc->completion);
  840. return 0;
  841. }
  842. /**
  843. * work_on_cpu - run a function in user context on a particular cpu
  844. * @cpu: the cpu to run on
  845. * @fn: the function to run
  846. * @arg: the function arg
  847. *
  848. * This will return the value @fn returns.
  849. * It is up to the caller to ensure that the cpu doesn't go offline.
  850. * The caller must not hold any locks which would prevent @fn from completing.
  851. */
  852. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  853. {
  854. struct task_struct *sub_thread;
  855. struct work_for_cpu wfc = {
  856. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  857. .fn = fn,
  858. .arg = arg,
  859. };
  860. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  861. if (IS_ERR(sub_thread))
  862. return PTR_ERR(sub_thread);
  863. kthread_bind(sub_thread, cpu);
  864. wake_up_process(sub_thread);
  865. wait_for_completion(&wfc.completion);
  866. return wfc.ret;
  867. }
  868. EXPORT_SYMBOL_GPL(work_on_cpu);
  869. #endif /* CONFIG_SMP */
  870. void __init init_workqueues(void)
  871. {
  872. alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL);
  873. cpumask_copy(cpu_populated_map, cpu_online_mask);
  874. singlethread_cpu = cpumask_first(cpu_possible_mask);
  875. cpu_singlethread_map = cpumask_of(singlethread_cpu);
  876. hotcpu_notifier(workqueue_cpu_callback, 0);
  877. keventd_wq = create_workqueue("events");
  878. BUG_ON(!keventd_wq);
  879. }