workqueue.c 25 KB

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