workqueue.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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. #include <linux/idr.h>
  36. /*
  37. * Structure fields follow one of the following exclusion rules.
  38. *
  39. * I: Set during initialization and read-only afterwards.
  40. *
  41. * L: cwq->lock protected. Access with cwq->lock held.
  42. *
  43. * F: wq->flush_mutex protected.
  44. *
  45. * W: workqueue_lock protected.
  46. */
  47. struct cpu_workqueue_struct;
  48. struct worker {
  49. struct work_struct *current_work; /* L: work being processed */
  50. struct list_head scheduled; /* L: scheduled works */
  51. struct task_struct *task; /* I: worker task */
  52. struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
  53. int id; /* I: worker id */
  54. };
  55. /*
  56. * The per-CPU workqueue (if single thread, we always use the first
  57. * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
  58. * work_struct->data are used for flags and thus cwqs need to be
  59. * aligned at two's power of the number of flag bits.
  60. */
  61. struct cpu_workqueue_struct {
  62. spinlock_t lock;
  63. struct list_head worklist;
  64. wait_queue_head_t more_work;
  65. unsigned int cpu;
  66. struct worker *worker;
  67. struct workqueue_struct *wq; /* I: the owning workqueue */
  68. int work_color; /* L: current color */
  69. int flush_color; /* L: flushing color */
  70. int nr_in_flight[WORK_NR_COLORS];
  71. /* L: nr of in_flight works */
  72. int nr_active; /* L: nr of active works */
  73. int max_active; /* L: max active works */
  74. struct list_head delayed_works; /* L: delayed works */
  75. };
  76. /*
  77. * Structure used to wait for workqueue flush.
  78. */
  79. struct wq_flusher {
  80. struct list_head list; /* F: list of flushers */
  81. int flush_color; /* F: flush color waiting for */
  82. struct completion done; /* flush completion */
  83. };
  84. /*
  85. * The externally visible workqueue abstraction is an array of
  86. * per-CPU workqueues:
  87. */
  88. struct workqueue_struct {
  89. unsigned int flags; /* I: WQ_* flags */
  90. struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
  91. struct list_head list; /* W: list of all workqueues */
  92. struct mutex flush_mutex; /* protects wq flushing */
  93. int work_color; /* F: current work color */
  94. int flush_color; /* F: current flush color */
  95. atomic_t nr_cwqs_to_flush; /* flush in progress */
  96. struct wq_flusher *first_flusher; /* F: first flusher */
  97. struct list_head flusher_queue; /* F: flush waiters */
  98. struct list_head flusher_overflow; /* F: flush overflow list */
  99. int saved_max_active; /* I: saved cwq max_active */
  100. const char *name; /* I: workqueue name */
  101. #ifdef CONFIG_LOCKDEP
  102. struct lockdep_map lockdep_map;
  103. #endif
  104. };
  105. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  106. static struct debug_obj_descr work_debug_descr;
  107. /*
  108. * fixup_init is called when:
  109. * - an active object is initialized
  110. */
  111. static int work_fixup_init(void *addr, enum debug_obj_state state)
  112. {
  113. struct work_struct *work = addr;
  114. switch (state) {
  115. case ODEBUG_STATE_ACTIVE:
  116. cancel_work_sync(work);
  117. debug_object_init(work, &work_debug_descr);
  118. return 1;
  119. default:
  120. return 0;
  121. }
  122. }
  123. /*
  124. * fixup_activate is called when:
  125. * - an active object is activated
  126. * - an unknown object is activated (might be a statically initialized object)
  127. */
  128. static int work_fixup_activate(void *addr, enum debug_obj_state state)
  129. {
  130. struct work_struct *work = addr;
  131. switch (state) {
  132. case ODEBUG_STATE_NOTAVAILABLE:
  133. /*
  134. * This is not really a fixup. The work struct was
  135. * statically initialized. We just make sure that it
  136. * is tracked in the object tracker.
  137. */
  138. if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
  139. debug_object_init(work, &work_debug_descr);
  140. debug_object_activate(work, &work_debug_descr);
  141. return 0;
  142. }
  143. WARN_ON_ONCE(1);
  144. return 0;
  145. case ODEBUG_STATE_ACTIVE:
  146. WARN_ON(1);
  147. default:
  148. return 0;
  149. }
  150. }
  151. /*
  152. * fixup_free is called when:
  153. * - an active object is freed
  154. */
  155. static int work_fixup_free(void *addr, enum debug_obj_state state)
  156. {
  157. struct work_struct *work = addr;
  158. switch (state) {
  159. case ODEBUG_STATE_ACTIVE:
  160. cancel_work_sync(work);
  161. debug_object_free(work, &work_debug_descr);
  162. return 1;
  163. default:
  164. return 0;
  165. }
  166. }
  167. static struct debug_obj_descr work_debug_descr = {
  168. .name = "work_struct",
  169. .fixup_init = work_fixup_init,
  170. .fixup_activate = work_fixup_activate,
  171. .fixup_free = work_fixup_free,
  172. };
  173. static inline void debug_work_activate(struct work_struct *work)
  174. {
  175. debug_object_activate(work, &work_debug_descr);
  176. }
  177. static inline void debug_work_deactivate(struct work_struct *work)
  178. {
  179. debug_object_deactivate(work, &work_debug_descr);
  180. }
  181. void __init_work(struct work_struct *work, int onstack)
  182. {
  183. if (onstack)
  184. debug_object_init_on_stack(work, &work_debug_descr);
  185. else
  186. debug_object_init(work, &work_debug_descr);
  187. }
  188. EXPORT_SYMBOL_GPL(__init_work);
  189. void destroy_work_on_stack(struct work_struct *work)
  190. {
  191. debug_object_free(work, &work_debug_descr);
  192. }
  193. EXPORT_SYMBOL_GPL(destroy_work_on_stack);
  194. #else
  195. static inline void debug_work_activate(struct work_struct *work) { }
  196. static inline void debug_work_deactivate(struct work_struct *work) { }
  197. #endif
  198. /* Serializes the accesses to the list of workqueues. */
  199. static DEFINE_SPINLOCK(workqueue_lock);
  200. static LIST_HEAD(workqueues);
  201. static DEFINE_PER_CPU(struct ida, worker_ida);
  202. static bool workqueue_freezing; /* W: have wqs started freezing? */
  203. static int worker_thread(void *__worker);
  204. static int singlethread_cpu __read_mostly;
  205. static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
  206. struct workqueue_struct *wq)
  207. {
  208. return per_cpu_ptr(wq->cpu_wq, cpu);
  209. }
  210. static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
  211. struct workqueue_struct *wq)
  212. {
  213. if (unlikely(wq->flags & WQ_SINGLE_THREAD))
  214. cpu = singlethread_cpu;
  215. return get_cwq(cpu, wq);
  216. }
  217. static unsigned int work_color_to_flags(int color)
  218. {
  219. return color << WORK_STRUCT_COLOR_SHIFT;
  220. }
  221. static int get_work_color(struct work_struct *work)
  222. {
  223. return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
  224. ((1 << WORK_STRUCT_COLOR_BITS) - 1);
  225. }
  226. static int work_next_color(int color)
  227. {
  228. return (color + 1) % WORK_NR_COLORS;
  229. }
  230. /*
  231. * Set the workqueue on which a work item is to be run
  232. * - Must *only* be called if the pending flag is set
  233. */
  234. static inline void set_wq_data(struct work_struct *work,
  235. struct cpu_workqueue_struct *cwq,
  236. unsigned long extra_flags)
  237. {
  238. BUG_ON(!work_pending(work));
  239. atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
  240. WORK_STRUCT_PENDING | extra_flags);
  241. }
  242. /*
  243. * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
  244. */
  245. static inline void clear_wq_data(struct work_struct *work)
  246. {
  247. atomic_long_set(&work->data, work_static(work));
  248. }
  249. static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
  250. {
  251. return (void *)(atomic_long_read(&work->data) &
  252. WORK_STRUCT_WQ_DATA_MASK);
  253. }
  254. /**
  255. * insert_work - insert a work into cwq
  256. * @cwq: cwq @work belongs to
  257. * @work: work to insert
  258. * @head: insertion point
  259. * @extra_flags: extra WORK_STRUCT_* flags to set
  260. *
  261. * Insert @work into @cwq after @head.
  262. *
  263. * CONTEXT:
  264. * spin_lock_irq(cwq->lock).
  265. */
  266. static void insert_work(struct cpu_workqueue_struct *cwq,
  267. struct work_struct *work, struct list_head *head,
  268. unsigned int extra_flags)
  269. {
  270. /* we own @work, set data and link */
  271. set_wq_data(work, cwq, extra_flags);
  272. /*
  273. * Ensure that we get the right work->data if we see the
  274. * result of list_add() below, see try_to_grab_pending().
  275. */
  276. smp_wmb();
  277. list_add_tail(&work->entry, head);
  278. wake_up(&cwq->more_work);
  279. }
  280. static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
  281. struct work_struct *work)
  282. {
  283. struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
  284. struct list_head *worklist;
  285. unsigned long flags;
  286. debug_work_activate(work);
  287. spin_lock_irqsave(&cwq->lock, flags);
  288. BUG_ON(!list_empty(&work->entry));
  289. cwq->nr_in_flight[cwq->work_color]++;
  290. if (likely(cwq->nr_active < cwq->max_active)) {
  291. cwq->nr_active++;
  292. worklist = &cwq->worklist;
  293. } else
  294. worklist = &cwq->delayed_works;
  295. insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
  296. spin_unlock_irqrestore(&cwq->lock, flags);
  297. }
  298. /**
  299. * queue_work - queue work on a workqueue
  300. * @wq: workqueue to use
  301. * @work: work to queue
  302. *
  303. * Returns 0 if @work was already on a queue, non-zero otherwise.
  304. *
  305. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  306. * it can be processed by another CPU.
  307. */
  308. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  309. {
  310. int ret;
  311. ret = queue_work_on(get_cpu(), wq, work);
  312. put_cpu();
  313. return ret;
  314. }
  315. EXPORT_SYMBOL_GPL(queue_work);
  316. /**
  317. * queue_work_on - queue work on specific cpu
  318. * @cpu: CPU number to execute work on
  319. * @wq: workqueue to use
  320. * @work: work to queue
  321. *
  322. * Returns 0 if @work was already on a queue, non-zero otherwise.
  323. *
  324. * We queue the work to a specific CPU, the caller must ensure it
  325. * can't go away.
  326. */
  327. int
  328. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  329. {
  330. int ret = 0;
  331. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  332. __queue_work(cpu, wq, work);
  333. ret = 1;
  334. }
  335. return ret;
  336. }
  337. EXPORT_SYMBOL_GPL(queue_work_on);
  338. static void delayed_work_timer_fn(unsigned long __data)
  339. {
  340. struct delayed_work *dwork = (struct delayed_work *)__data;
  341. struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
  342. __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
  343. }
  344. /**
  345. * queue_delayed_work - queue work on a workqueue after delay
  346. * @wq: workqueue to use
  347. * @dwork: delayable work to queue
  348. * @delay: number of jiffies to wait before queueing
  349. *
  350. * Returns 0 if @work was already on a queue, non-zero otherwise.
  351. */
  352. int queue_delayed_work(struct workqueue_struct *wq,
  353. struct delayed_work *dwork, unsigned long delay)
  354. {
  355. if (delay == 0)
  356. return queue_work(wq, &dwork->work);
  357. return queue_delayed_work_on(-1, wq, dwork, delay);
  358. }
  359. EXPORT_SYMBOL_GPL(queue_delayed_work);
  360. /**
  361. * queue_delayed_work_on - queue work on specific CPU after delay
  362. * @cpu: CPU number to execute work on
  363. * @wq: workqueue to use
  364. * @dwork: work to queue
  365. * @delay: number of jiffies to wait before queueing
  366. *
  367. * Returns 0 if @work was already on a queue, non-zero otherwise.
  368. */
  369. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  370. struct delayed_work *dwork, unsigned long delay)
  371. {
  372. int ret = 0;
  373. struct timer_list *timer = &dwork->timer;
  374. struct work_struct *work = &dwork->work;
  375. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  376. BUG_ON(timer_pending(timer));
  377. BUG_ON(!list_empty(&work->entry));
  378. timer_stats_timer_set_start_info(&dwork->timer);
  379. /* This stores cwq for the moment, for the timer_fn */
  380. set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
  381. timer->expires = jiffies + delay;
  382. timer->data = (unsigned long)dwork;
  383. timer->function = delayed_work_timer_fn;
  384. if (unlikely(cpu >= 0))
  385. add_timer_on(timer, cpu);
  386. else
  387. add_timer(timer);
  388. ret = 1;
  389. }
  390. return ret;
  391. }
  392. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  393. static struct worker *alloc_worker(void)
  394. {
  395. struct worker *worker;
  396. worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  397. if (worker)
  398. INIT_LIST_HEAD(&worker->scheduled);
  399. return worker;
  400. }
  401. /**
  402. * create_worker - create a new workqueue worker
  403. * @cwq: cwq the new worker will belong to
  404. * @bind: whether to set affinity to @cpu or not
  405. *
  406. * Create a new worker which is bound to @cwq. The returned worker
  407. * can be started by calling start_worker() or destroyed using
  408. * destroy_worker().
  409. *
  410. * CONTEXT:
  411. * Might sleep. Does GFP_KERNEL allocations.
  412. *
  413. * RETURNS:
  414. * Pointer to the newly created worker.
  415. */
  416. static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
  417. {
  418. int id = -1;
  419. struct worker *worker = NULL;
  420. spin_lock(&workqueue_lock);
  421. while (ida_get_new(&per_cpu(worker_ida, cwq->cpu), &id)) {
  422. spin_unlock(&workqueue_lock);
  423. if (!ida_pre_get(&per_cpu(worker_ida, cwq->cpu), GFP_KERNEL))
  424. goto fail;
  425. spin_lock(&workqueue_lock);
  426. }
  427. spin_unlock(&workqueue_lock);
  428. worker = alloc_worker();
  429. if (!worker)
  430. goto fail;
  431. worker->cwq = cwq;
  432. worker->id = id;
  433. worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
  434. cwq->cpu, id);
  435. if (IS_ERR(worker->task))
  436. goto fail;
  437. if (bind)
  438. kthread_bind(worker->task, cwq->cpu);
  439. return worker;
  440. fail:
  441. if (id >= 0) {
  442. spin_lock(&workqueue_lock);
  443. ida_remove(&per_cpu(worker_ida, cwq->cpu), id);
  444. spin_unlock(&workqueue_lock);
  445. }
  446. kfree(worker);
  447. return NULL;
  448. }
  449. /**
  450. * start_worker - start a newly created worker
  451. * @worker: worker to start
  452. *
  453. * Start @worker.
  454. *
  455. * CONTEXT:
  456. * spin_lock_irq(cwq->lock).
  457. */
  458. static void start_worker(struct worker *worker)
  459. {
  460. wake_up_process(worker->task);
  461. }
  462. /**
  463. * destroy_worker - destroy a workqueue worker
  464. * @worker: worker to be destroyed
  465. *
  466. * Destroy @worker.
  467. */
  468. static void destroy_worker(struct worker *worker)
  469. {
  470. int cpu = worker->cwq->cpu;
  471. int id = worker->id;
  472. /* sanity check frenzy */
  473. BUG_ON(worker->current_work);
  474. BUG_ON(!list_empty(&worker->scheduled));
  475. kthread_stop(worker->task);
  476. kfree(worker);
  477. spin_lock(&workqueue_lock);
  478. ida_remove(&per_cpu(worker_ida, cpu), id);
  479. spin_unlock(&workqueue_lock);
  480. }
  481. /**
  482. * move_linked_works - move linked works to a list
  483. * @work: start of series of works to be scheduled
  484. * @head: target list to append @work to
  485. * @nextp: out paramter for nested worklist walking
  486. *
  487. * Schedule linked works starting from @work to @head. Work series to
  488. * be scheduled starts at @work and includes any consecutive work with
  489. * WORK_STRUCT_LINKED set in its predecessor.
  490. *
  491. * If @nextp is not NULL, it's updated to point to the next work of
  492. * the last scheduled work. This allows move_linked_works() to be
  493. * nested inside outer list_for_each_entry_safe().
  494. *
  495. * CONTEXT:
  496. * spin_lock_irq(cwq->lock).
  497. */
  498. static void move_linked_works(struct work_struct *work, struct list_head *head,
  499. struct work_struct **nextp)
  500. {
  501. struct work_struct *n;
  502. /*
  503. * Linked worklist will always end before the end of the list,
  504. * use NULL for list head.
  505. */
  506. list_for_each_entry_safe_from(work, n, NULL, entry) {
  507. list_move_tail(&work->entry, head);
  508. if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
  509. break;
  510. }
  511. /*
  512. * If we're already inside safe list traversal and have moved
  513. * multiple works to the scheduled queue, the next position
  514. * needs to be updated.
  515. */
  516. if (nextp)
  517. *nextp = n;
  518. }
  519. static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
  520. {
  521. struct work_struct *work = list_first_entry(&cwq->delayed_works,
  522. struct work_struct, entry);
  523. move_linked_works(work, &cwq->worklist, NULL);
  524. cwq->nr_active++;
  525. }
  526. /**
  527. * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
  528. * @cwq: cwq of interest
  529. * @color: color of work which left the queue
  530. *
  531. * A work either has completed or is removed from pending queue,
  532. * decrement nr_in_flight of its cwq and handle workqueue flushing.
  533. *
  534. * CONTEXT:
  535. * spin_lock_irq(cwq->lock).
  536. */
  537. static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
  538. {
  539. /* ignore uncolored works */
  540. if (color == WORK_NO_COLOR)
  541. return;
  542. cwq->nr_in_flight[color]--;
  543. cwq->nr_active--;
  544. /* one down, submit a delayed one */
  545. if (!list_empty(&cwq->delayed_works) &&
  546. cwq->nr_active < cwq->max_active)
  547. cwq_activate_first_delayed(cwq);
  548. /* is flush in progress and are we at the flushing tip? */
  549. if (likely(cwq->flush_color != color))
  550. return;
  551. /* are there still in-flight works? */
  552. if (cwq->nr_in_flight[color])
  553. return;
  554. /* this cwq is done, clear flush_color */
  555. cwq->flush_color = -1;
  556. /*
  557. * If this was the last cwq, wake up the first flusher. It
  558. * will handle the rest.
  559. */
  560. if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
  561. complete(&cwq->wq->first_flusher->done);
  562. }
  563. /**
  564. * process_one_work - process single work
  565. * @worker: self
  566. * @work: work to process
  567. *
  568. * Process @work. This function contains all the logics necessary to
  569. * process a single work including synchronization against and
  570. * interaction with other workers on the same cpu, queueing and
  571. * flushing. As long as context requirement is met, any worker can
  572. * call this function to process a work.
  573. *
  574. * CONTEXT:
  575. * spin_lock_irq(cwq->lock) which is released and regrabbed.
  576. */
  577. static void process_one_work(struct worker *worker, struct work_struct *work)
  578. {
  579. struct cpu_workqueue_struct *cwq = worker->cwq;
  580. work_func_t f = work->func;
  581. int work_color;
  582. #ifdef CONFIG_LOCKDEP
  583. /*
  584. * It is permissible to free the struct work_struct from
  585. * inside the function that is called from it, this we need to
  586. * take into account for lockdep too. To avoid bogus "held
  587. * lock freed" warnings as well as problems when looking into
  588. * work->lockdep_map, make a copy and use that here.
  589. */
  590. struct lockdep_map lockdep_map = work->lockdep_map;
  591. #endif
  592. /* claim and process */
  593. debug_work_deactivate(work);
  594. worker->current_work = work;
  595. work_color = get_work_color(work);
  596. list_del_init(&work->entry);
  597. spin_unlock_irq(&cwq->lock);
  598. BUG_ON(get_wq_data(work) != cwq);
  599. work_clear_pending(work);
  600. lock_map_acquire(&cwq->wq->lockdep_map);
  601. lock_map_acquire(&lockdep_map);
  602. f(work);
  603. lock_map_release(&lockdep_map);
  604. lock_map_release(&cwq->wq->lockdep_map);
  605. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  606. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  607. "%s/0x%08x/%d\n",
  608. current->comm, preempt_count(), task_pid_nr(current));
  609. printk(KERN_ERR " last function: ");
  610. print_symbol("%s\n", (unsigned long)f);
  611. debug_show_held_locks(current);
  612. dump_stack();
  613. }
  614. spin_lock_irq(&cwq->lock);
  615. /* we're done with it, release */
  616. worker->current_work = NULL;
  617. cwq_dec_nr_in_flight(cwq, work_color);
  618. }
  619. /**
  620. * process_scheduled_works - process scheduled works
  621. * @worker: self
  622. *
  623. * Process all scheduled works. Please note that the scheduled list
  624. * may change while processing a work, so this function repeatedly
  625. * fetches a work from the top and executes it.
  626. *
  627. * CONTEXT:
  628. * spin_lock_irq(cwq->lock) which may be released and regrabbed
  629. * multiple times.
  630. */
  631. static void process_scheduled_works(struct worker *worker)
  632. {
  633. while (!list_empty(&worker->scheduled)) {
  634. struct work_struct *work = list_first_entry(&worker->scheduled,
  635. struct work_struct, entry);
  636. process_one_work(worker, work);
  637. }
  638. }
  639. /**
  640. * worker_thread - the worker thread function
  641. * @__worker: self
  642. *
  643. * The cwq worker thread function.
  644. */
  645. static int worker_thread(void *__worker)
  646. {
  647. struct worker *worker = __worker;
  648. struct cpu_workqueue_struct *cwq = worker->cwq;
  649. DEFINE_WAIT(wait);
  650. for (;;) {
  651. prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
  652. if (!kthread_should_stop() &&
  653. list_empty(&cwq->worklist))
  654. schedule();
  655. finish_wait(&cwq->more_work, &wait);
  656. if (kthread_should_stop())
  657. break;
  658. if (unlikely(!cpumask_equal(&worker->task->cpus_allowed,
  659. get_cpu_mask(cwq->cpu))))
  660. set_cpus_allowed_ptr(worker->task,
  661. get_cpu_mask(cwq->cpu));
  662. spin_lock_irq(&cwq->lock);
  663. while (!list_empty(&cwq->worklist)) {
  664. struct work_struct *work =
  665. list_first_entry(&cwq->worklist,
  666. struct work_struct, entry);
  667. if (likely(!(*work_data_bits(work) &
  668. WORK_STRUCT_LINKED))) {
  669. /* optimization path, not strictly necessary */
  670. process_one_work(worker, work);
  671. if (unlikely(!list_empty(&worker->scheduled)))
  672. process_scheduled_works(worker);
  673. } else {
  674. move_linked_works(work, &worker->scheduled,
  675. NULL);
  676. process_scheduled_works(worker);
  677. }
  678. }
  679. spin_unlock_irq(&cwq->lock);
  680. }
  681. return 0;
  682. }
  683. struct wq_barrier {
  684. struct work_struct work;
  685. struct completion done;
  686. };
  687. static void wq_barrier_func(struct work_struct *work)
  688. {
  689. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  690. complete(&barr->done);
  691. }
  692. /**
  693. * insert_wq_barrier - insert a barrier work
  694. * @cwq: cwq to insert barrier into
  695. * @barr: wq_barrier to insert
  696. * @target: target work to attach @barr to
  697. * @worker: worker currently executing @target, NULL if @target is not executing
  698. *
  699. * @barr is linked to @target such that @barr is completed only after
  700. * @target finishes execution. Please note that the ordering
  701. * guarantee is observed only with respect to @target and on the local
  702. * cpu.
  703. *
  704. * Currently, a queued barrier can't be canceled. This is because
  705. * try_to_grab_pending() can't determine whether the work to be
  706. * grabbed is at the head of the queue and thus can't clear LINKED
  707. * flag of the previous work while there must be a valid next work
  708. * after a work with LINKED flag set.
  709. *
  710. * Note that when @worker is non-NULL, @target may be modified
  711. * underneath us, so we can't reliably determine cwq from @target.
  712. *
  713. * CONTEXT:
  714. * spin_lock_irq(cwq->lock).
  715. */
  716. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  717. struct wq_barrier *barr,
  718. struct work_struct *target, struct worker *worker)
  719. {
  720. struct list_head *head;
  721. unsigned int linked = 0;
  722. /*
  723. * debugobject calls are safe here even with cwq->lock locked
  724. * as we know for sure that this will not trigger any of the
  725. * checks and call back into the fixup functions where we
  726. * might deadlock.
  727. */
  728. INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
  729. __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
  730. init_completion(&barr->done);
  731. /*
  732. * If @target is currently being executed, schedule the
  733. * barrier to the worker; otherwise, put it after @target.
  734. */
  735. if (worker)
  736. head = worker->scheduled.next;
  737. else {
  738. unsigned long *bits = work_data_bits(target);
  739. head = target->entry.next;
  740. /* there can already be other linked works, inherit and set */
  741. linked = *bits & WORK_STRUCT_LINKED;
  742. __set_bit(WORK_STRUCT_LINKED_BIT, bits);
  743. }
  744. debug_work_activate(&barr->work);
  745. insert_work(cwq, &barr->work, head,
  746. work_color_to_flags(WORK_NO_COLOR) | linked);
  747. }
  748. /**
  749. * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
  750. * @wq: workqueue being flushed
  751. * @flush_color: new flush color, < 0 for no-op
  752. * @work_color: new work color, < 0 for no-op
  753. *
  754. * Prepare cwqs for workqueue flushing.
  755. *
  756. * If @flush_color is non-negative, flush_color on all cwqs should be
  757. * -1. If no cwq has in-flight commands at the specified color, all
  758. * cwq->flush_color's stay at -1 and %false is returned. If any cwq
  759. * has in flight commands, its cwq->flush_color is set to
  760. * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
  761. * wakeup logic is armed and %true is returned.
  762. *
  763. * The caller should have initialized @wq->first_flusher prior to
  764. * calling this function with non-negative @flush_color. If
  765. * @flush_color is negative, no flush color update is done and %false
  766. * is returned.
  767. *
  768. * If @work_color is non-negative, all cwqs should have the same
  769. * work_color which is previous to @work_color and all will be
  770. * advanced to @work_color.
  771. *
  772. * CONTEXT:
  773. * mutex_lock(wq->flush_mutex).
  774. *
  775. * RETURNS:
  776. * %true if @flush_color >= 0 and there's something to flush. %false
  777. * otherwise.
  778. */
  779. static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
  780. int flush_color, int work_color)
  781. {
  782. bool wait = false;
  783. unsigned int cpu;
  784. if (flush_color >= 0) {
  785. BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
  786. atomic_set(&wq->nr_cwqs_to_flush, 1);
  787. }
  788. for_each_possible_cpu(cpu) {
  789. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  790. spin_lock_irq(&cwq->lock);
  791. if (flush_color >= 0) {
  792. BUG_ON(cwq->flush_color != -1);
  793. if (cwq->nr_in_flight[flush_color]) {
  794. cwq->flush_color = flush_color;
  795. atomic_inc(&wq->nr_cwqs_to_flush);
  796. wait = true;
  797. }
  798. }
  799. if (work_color >= 0) {
  800. BUG_ON(work_color != work_next_color(cwq->work_color));
  801. cwq->work_color = work_color;
  802. }
  803. spin_unlock_irq(&cwq->lock);
  804. }
  805. if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
  806. complete(&wq->first_flusher->done);
  807. return wait;
  808. }
  809. /**
  810. * flush_workqueue - ensure that any scheduled work has run to completion.
  811. * @wq: workqueue to flush
  812. *
  813. * Forces execution of the workqueue and blocks until its completion.
  814. * This is typically used in driver shutdown handlers.
  815. *
  816. * We sleep until all works which were queued on entry have been handled,
  817. * but we are not livelocked by new incoming ones.
  818. */
  819. void flush_workqueue(struct workqueue_struct *wq)
  820. {
  821. struct wq_flusher this_flusher = {
  822. .list = LIST_HEAD_INIT(this_flusher.list),
  823. .flush_color = -1,
  824. .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
  825. };
  826. int next_color;
  827. lock_map_acquire(&wq->lockdep_map);
  828. lock_map_release(&wq->lockdep_map);
  829. mutex_lock(&wq->flush_mutex);
  830. /*
  831. * Start-to-wait phase
  832. */
  833. next_color = work_next_color(wq->work_color);
  834. if (next_color != wq->flush_color) {
  835. /*
  836. * Color space is not full. The current work_color
  837. * becomes our flush_color and work_color is advanced
  838. * by one.
  839. */
  840. BUG_ON(!list_empty(&wq->flusher_overflow));
  841. this_flusher.flush_color = wq->work_color;
  842. wq->work_color = next_color;
  843. if (!wq->first_flusher) {
  844. /* no flush in progress, become the first flusher */
  845. BUG_ON(wq->flush_color != this_flusher.flush_color);
  846. wq->first_flusher = &this_flusher;
  847. if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
  848. wq->work_color)) {
  849. /* nothing to flush, done */
  850. wq->flush_color = next_color;
  851. wq->first_flusher = NULL;
  852. goto out_unlock;
  853. }
  854. } else {
  855. /* wait in queue */
  856. BUG_ON(wq->flush_color == this_flusher.flush_color);
  857. list_add_tail(&this_flusher.list, &wq->flusher_queue);
  858. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  859. }
  860. } else {
  861. /*
  862. * Oops, color space is full, wait on overflow queue.
  863. * The next flush completion will assign us
  864. * flush_color and transfer to flusher_queue.
  865. */
  866. list_add_tail(&this_flusher.list, &wq->flusher_overflow);
  867. }
  868. mutex_unlock(&wq->flush_mutex);
  869. wait_for_completion(&this_flusher.done);
  870. /*
  871. * Wake-up-and-cascade phase
  872. *
  873. * First flushers are responsible for cascading flushes and
  874. * handling overflow. Non-first flushers can simply return.
  875. */
  876. if (wq->first_flusher != &this_flusher)
  877. return;
  878. mutex_lock(&wq->flush_mutex);
  879. wq->first_flusher = NULL;
  880. BUG_ON(!list_empty(&this_flusher.list));
  881. BUG_ON(wq->flush_color != this_flusher.flush_color);
  882. while (true) {
  883. struct wq_flusher *next, *tmp;
  884. /* complete all the flushers sharing the current flush color */
  885. list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
  886. if (next->flush_color != wq->flush_color)
  887. break;
  888. list_del_init(&next->list);
  889. complete(&next->done);
  890. }
  891. BUG_ON(!list_empty(&wq->flusher_overflow) &&
  892. wq->flush_color != work_next_color(wq->work_color));
  893. /* this flush_color is finished, advance by one */
  894. wq->flush_color = work_next_color(wq->flush_color);
  895. /* one color has been freed, handle overflow queue */
  896. if (!list_empty(&wq->flusher_overflow)) {
  897. /*
  898. * Assign the same color to all overflowed
  899. * flushers, advance work_color and append to
  900. * flusher_queue. This is the start-to-wait
  901. * phase for these overflowed flushers.
  902. */
  903. list_for_each_entry(tmp, &wq->flusher_overflow, list)
  904. tmp->flush_color = wq->work_color;
  905. wq->work_color = work_next_color(wq->work_color);
  906. list_splice_tail_init(&wq->flusher_overflow,
  907. &wq->flusher_queue);
  908. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  909. }
  910. if (list_empty(&wq->flusher_queue)) {
  911. BUG_ON(wq->flush_color != wq->work_color);
  912. break;
  913. }
  914. /*
  915. * Need to flush more colors. Make the next flusher
  916. * the new first flusher and arm cwqs.
  917. */
  918. BUG_ON(wq->flush_color == wq->work_color);
  919. BUG_ON(wq->flush_color != next->flush_color);
  920. list_del_init(&next->list);
  921. wq->first_flusher = next;
  922. if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
  923. break;
  924. /*
  925. * Meh... this color is already done, clear first
  926. * flusher and repeat cascading.
  927. */
  928. wq->first_flusher = NULL;
  929. }
  930. out_unlock:
  931. mutex_unlock(&wq->flush_mutex);
  932. }
  933. EXPORT_SYMBOL_GPL(flush_workqueue);
  934. /**
  935. * flush_work - block until a work_struct's callback has terminated
  936. * @work: the work which is to be flushed
  937. *
  938. * Returns false if @work has already terminated.
  939. *
  940. * It is expected that, prior to calling flush_work(), the caller has
  941. * arranged for the work to not be requeued, otherwise it doesn't make
  942. * sense to use this function.
  943. */
  944. int flush_work(struct work_struct *work)
  945. {
  946. struct worker *worker = NULL;
  947. struct cpu_workqueue_struct *cwq;
  948. struct wq_barrier barr;
  949. might_sleep();
  950. cwq = get_wq_data(work);
  951. if (!cwq)
  952. return 0;
  953. lock_map_acquire(&cwq->wq->lockdep_map);
  954. lock_map_release(&cwq->wq->lockdep_map);
  955. spin_lock_irq(&cwq->lock);
  956. if (!list_empty(&work->entry)) {
  957. /*
  958. * See the comment near try_to_grab_pending()->smp_rmb().
  959. * If it was re-queued under us we are not going to wait.
  960. */
  961. smp_rmb();
  962. if (unlikely(cwq != get_wq_data(work)))
  963. goto already_gone;
  964. } else {
  965. if (cwq->worker && cwq->worker->current_work == work)
  966. worker = cwq->worker;
  967. if (!worker)
  968. goto already_gone;
  969. }
  970. insert_wq_barrier(cwq, &barr, work, worker);
  971. spin_unlock_irq(&cwq->lock);
  972. wait_for_completion(&barr.done);
  973. destroy_work_on_stack(&barr.work);
  974. return 1;
  975. already_gone:
  976. spin_unlock_irq(&cwq->lock);
  977. return 0;
  978. }
  979. EXPORT_SYMBOL_GPL(flush_work);
  980. /*
  981. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  982. * so this work can't be re-armed in any way.
  983. */
  984. static int try_to_grab_pending(struct work_struct *work)
  985. {
  986. struct cpu_workqueue_struct *cwq;
  987. int ret = -1;
  988. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  989. return 0;
  990. /*
  991. * The queueing is in progress, or it is already queued. Try to
  992. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  993. */
  994. cwq = get_wq_data(work);
  995. if (!cwq)
  996. return ret;
  997. spin_lock_irq(&cwq->lock);
  998. if (!list_empty(&work->entry)) {
  999. /*
  1000. * This work is queued, but perhaps we locked the wrong cwq.
  1001. * In that case we must see the new value after rmb(), see
  1002. * insert_work()->wmb().
  1003. */
  1004. smp_rmb();
  1005. if (cwq == get_wq_data(work)) {
  1006. debug_work_deactivate(work);
  1007. list_del_init(&work->entry);
  1008. cwq_dec_nr_in_flight(cwq, get_work_color(work));
  1009. ret = 1;
  1010. }
  1011. }
  1012. spin_unlock_irq(&cwq->lock);
  1013. return ret;
  1014. }
  1015. static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
  1016. struct work_struct *work)
  1017. {
  1018. struct wq_barrier barr;
  1019. struct worker *worker;
  1020. spin_lock_irq(&cwq->lock);
  1021. worker = NULL;
  1022. if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
  1023. worker = cwq->worker;
  1024. insert_wq_barrier(cwq, &barr, work, worker);
  1025. }
  1026. spin_unlock_irq(&cwq->lock);
  1027. if (unlikely(worker)) {
  1028. wait_for_completion(&barr.done);
  1029. destroy_work_on_stack(&barr.work);
  1030. }
  1031. }
  1032. static void wait_on_work(struct work_struct *work)
  1033. {
  1034. struct cpu_workqueue_struct *cwq;
  1035. struct workqueue_struct *wq;
  1036. int cpu;
  1037. might_sleep();
  1038. lock_map_acquire(&work->lockdep_map);
  1039. lock_map_release(&work->lockdep_map);
  1040. cwq = get_wq_data(work);
  1041. if (!cwq)
  1042. return;
  1043. wq = cwq->wq;
  1044. for_each_possible_cpu(cpu)
  1045. wait_on_cpu_work(get_cwq(cpu, wq), work);
  1046. }
  1047. static int __cancel_work_timer(struct work_struct *work,
  1048. struct timer_list* timer)
  1049. {
  1050. int ret;
  1051. do {
  1052. ret = (timer && likely(del_timer(timer)));
  1053. if (!ret)
  1054. ret = try_to_grab_pending(work);
  1055. wait_on_work(work);
  1056. } while (unlikely(ret < 0));
  1057. clear_wq_data(work);
  1058. return ret;
  1059. }
  1060. /**
  1061. * cancel_work_sync - block until a work_struct's callback has terminated
  1062. * @work: the work which is to be flushed
  1063. *
  1064. * Returns true if @work was pending.
  1065. *
  1066. * cancel_work_sync() will cancel the work if it is queued. If the work's
  1067. * callback appears to be running, cancel_work_sync() will block until it
  1068. * has completed.
  1069. *
  1070. * It is possible to use this function if the work re-queues itself. It can
  1071. * cancel the work even if it migrates to another workqueue, however in that
  1072. * case it only guarantees that work->func() has completed on the last queued
  1073. * workqueue.
  1074. *
  1075. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  1076. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  1077. *
  1078. * The caller must ensure that workqueue_struct on which this work was last
  1079. * queued can't be destroyed before this function returns.
  1080. */
  1081. int cancel_work_sync(struct work_struct *work)
  1082. {
  1083. return __cancel_work_timer(work, NULL);
  1084. }
  1085. EXPORT_SYMBOL_GPL(cancel_work_sync);
  1086. /**
  1087. * cancel_delayed_work_sync - reliably kill off a delayed work.
  1088. * @dwork: the delayed work struct
  1089. *
  1090. * Returns true if @dwork was pending.
  1091. *
  1092. * It is possible to use this function if @dwork rearms itself via queue_work()
  1093. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  1094. */
  1095. int cancel_delayed_work_sync(struct delayed_work *dwork)
  1096. {
  1097. return __cancel_work_timer(&dwork->work, &dwork->timer);
  1098. }
  1099. EXPORT_SYMBOL(cancel_delayed_work_sync);
  1100. static struct workqueue_struct *keventd_wq __read_mostly;
  1101. /**
  1102. * schedule_work - put work task in global workqueue
  1103. * @work: job to be done
  1104. *
  1105. * Returns zero if @work was already on the kernel-global workqueue and
  1106. * non-zero otherwise.
  1107. *
  1108. * This puts a job in the kernel-global workqueue if it was not already
  1109. * queued and leaves it in the same position on the kernel-global
  1110. * workqueue otherwise.
  1111. */
  1112. int schedule_work(struct work_struct *work)
  1113. {
  1114. return queue_work(keventd_wq, work);
  1115. }
  1116. EXPORT_SYMBOL(schedule_work);
  1117. /*
  1118. * schedule_work_on - put work task on a specific cpu
  1119. * @cpu: cpu to put the work task on
  1120. * @work: job to be done
  1121. *
  1122. * This puts a job on a specific cpu
  1123. */
  1124. int schedule_work_on(int cpu, struct work_struct *work)
  1125. {
  1126. return queue_work_on(cpu, keventd_wq, work);
  1127. }
  1128. EXPORT_SYMBOL(schedule_work_on);
  1129. /**
  1130. * schedule_delayed_work - put work task in global workqueue after delay
  1131. * @dwork: job to be done
  1132. * @delay: number of jiffies to wait or 0 for immediate execution
  1133. *
  1134. * After waiting for a given time this puts a job in the kernel-global
  1135. * workqueue.
  1136. */
  1137. int schedule_delayed_work(struct delayed_work *dwork,
  1138. unsigned long delay)
  1139. {
  1140. return queue_delayed_work(keventd_wq, dwork, delay);
  1141. }
  1142. EXPORT_SYMBOL(schedule_delayed_work);
  1143. /**
  1144. * flush_delayed_work - block until a dwork_struct's callback has terminated
  1145. * @dwork: the delayed work which is to be flushed
  1146. *
  1147. * Any timeout is cancelled, and any pending work is run immediately.
  1148. */
  1149. void flush_delayed_work(struct delayed_work *dwork)
  1150. {
  1151. if (del_timer_sync(&dwork->timer)) {
  1152. __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
  1153. &dwork->work);
  1154. put_cpu();
  1155. }
  1156. flush_work(&dwork->work);
  1157. }
  1158. EXPORT_SYMBOL(flush_delayed_work);
  1159. /**
  1160. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  1161. * @cpu: cpu to use
  1162. * @dwork: job to be done
  1163. * @delay: number of jiffies to wait
  1164. *
  1165. * After waiting for a given time this puts a job in the kernel-global
  1166. * workqueue on the specified CPU.
  1167. */
  1168. int schedule_delayed_work_on(int cpu,
  1169. struct delayed_work *dwork, unsigned long delay)
  1170. {
  1171. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  1172. }
  1173. EXPORT_SYMBOL(schedule_delayed_work_on);
  1174. /**
  1175. * schedule_on_each_cpu - call a function on each online CPU from keventd
  1176. * @func: the function to call
  1177. *
  1178. * Returns zero on success.
  1179. * Returns -ve errno on failure.
  1180. *
  1181. * schedule_on_each_cpu() is very slow.
  1182. */
  1183. int schedule_on_each_cpu(work_func_t func)
  1184. {
  1185. int cpu;
  1186. int orig = -1;
  1187. struct work_struct *works;
  1188. works = alloc_percpu(struct work_struct);
  1189. if (!works)
  1190. return -ENOMEM;
  1191. get_online_cpus();
  1192. /*
  1193. * When running in keventd don't schedule a work item on
  1194. * itself. Can just call directly because the work queue is
  1195. * already bound. This also is faster.
  1196. */
  1197. if (current_is_keventd())
  1198. orig = raw_smp_processor_id();
  1199. for_each_online_cpu(cpu) {
  1200. struct work_struct *work = per_cpu_ptr(works, cpu);
  1201. INIT_WORK(work, func);
  1202. if (cpu != orig)
  1203. schedule_work_on(cpu, work);
  1204. }
  1205. if (orig >= 0)
  1206. func(per_cpu_ptr(works, orig));
  1207. for_each_online_cpu(cpu)
  1208. flush_work(per_cpu_ptr(works, cpu));
  1209. put_online_cpus();
  1210. free_percpu(works);
  1211. return 0;
  1212. }
  1213. /**
  1214. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  1215. *
  1216. * Forces execution of the kernel-global workqueue and blocks until its
  1217. * completion.
  1218. *
  1219. * Think twice before calling this function! It's very easy to get into
  1220. * trouble if you don't take great care. Either of the following situations
  1221. * will lead to deadlock:
  1222. *
  1223. * One of the work items currently on the workqueue needs to acquire
  1224. * a lock held by your code or its caller.
  1225. *
  1226. * Your code is running in the context of a work routine.
  1227. *
  1228. * They will be detected by lockdep when they occur, but the first might not
  1229. * occur very often. It depends on what work items are on the workqueue and
  1230. * what locks they need, which you have no control over.
  1231. *
  1232. * In most situations flushing the entire workqueue is overkill; you merely
  1233. * need to know that a particular work item isn't queued and isn't running.
  1234. * In such cases you should use cancel_delayed_work_sync() or
  1235. * cancel_work_sync() instead.
  1236. */
  1237. void flush_scheduled_work(void)
  1238. {
  1239. flush_workqueue(keventd_wq);
  1240. }
  1241. EXPORT_SYMBOL(flush_scheduled_work);
  1242. /**
  1243. * execute_in_process_context - reliably execute the routine with user context
  1244. * @fn: the function to execute
  1245. * @ew: guaranteed storage for the execute work structure (must
  1246. * be available when the work executes)
  1247. *
  1248. * Executes the function immediately if process context is available,
  1249. * otherwise schedules the function for delayed execution.
  1250. *
  1251. * Returns: 0 - function was executed
  1252. * 1 - function was scheduled for execution
  1253. */
  1254. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  1255. {
  1256. if (!in_interrupt()) {
  1257. fn(&ew->work);
  1258. return 0;
  1259. }
  1260. INIT_WORK(&ew->work, fn);
  1261. schedule_work(&ew->work);
  1262. return 1;
  1263. }
  1264. EXPORT_SYMBOL_GPL(execute_in_process_context);
  1265. int keventd_up(void)
  1266. {
  1267. return keventd_wq != NULL;
  1268. }
  1269. int current_is_keventd(void)
  1270. {
  1271. struct cpu_workqueue_struct *cwq;
  1272. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  1273. int ret = 0;
  1274. BUG_ON(!keventd_wq);
  1275. cwq = get_cwq(cpu, keventd_wq);
  1276. if (current == cwq->worker->task)
  1277. ret = 1;
  1278. return ret;
  1279. }
  1280. static struct cpu_workqueue_struct *alloc_cwqs(void)
  1281. {
  1282. /*
  1283. * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
  1284. * Make sure that the alignment isn't lower than that of
  1285. * unsigned long long.
  1286. */
  1287. const size_t size = sizeof(struct cpu_workqueue_struct);
  1288. const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
  1289. __alignof__(unsigned long long));
  1290. struct cpu_workqueue_struct *cwqs;
  1291. #ifndef CONFIG_SMP
  1292. void *ptr;
  1293. /*
  1294. * On UP, percpu allocator doesn't honor alignment parameter
  1295. * and simply uses arch-dependent default. Allocate enough
  1296. * room to align cwq and put an extra pointer at the end
  1297. * pointing back to the originally allocated pointer which
  1298. * will be used for free.
  1299. *
  1300. * FIXME: This really belongs to UP percpu code. Update UP
  1301. * percpu code to honor alignment and remove this ugliness.
  1302. */
  1303. ptr = __alloc_percpu(size + align + sizeof(void *), 1);
  1304. cwqs = PTR_ALIGN(ptr, align);
  1305. *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
  1306. #else
  1307. /* On SMP, percpu allocator can do it itself */
  1308. cwqs = __alloc_percpu(size, align);
  1309. #endif
  1310. /* just in case, make sure it's actually aligned */
  1311. BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
  1312. return cwqs;
  1313. }
  1314. static void free_cwqs(struct cpu_workqueue_struct *cwqs)
  1315. {
  1316. #ifndef CONFIG_SMP
  1317. /* on UP, the pointer to free is stored right after the cwq */
  1318. if (cwqs)
  1319. free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
  1320. #else
  1321. free_percpu(cwqs);
  1322. #endif
  1323. }
  1324. struct workqueue_struct *__create_workqueue_key(const char *name,
  1325. unsigned int flags,
  1326. int max_active,
  1327. struct lock_class_key *key,
  1328. const char *lock_name)
  1329. {
  1330. bool singlethread = flags & WQ_SINGLE_THREAD;
  1331. struct workqueue_struct *wq;
  1332. bool failed = false;
  1333. unsigned int cpu;
  1334. max_active = clamp_val(max_active, 1, INT_MAX);
  1335. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  1336. if (!wq)
  1337. goto err;
  1338. wq->cpu_wq = alloc_cwqs();
  1339. if (!wq->cpu_wq)
  1340. goto err;
  1341. wq->flags = flags;
  1342. wq->saved_max_active = max_active;
  1343. mutex_init(&wq->flush_mutex);
  1344. atomic_set(&wq->nr_cwqs_to_flush, 0);
  1345. INIT_LIST_HEAD(&wq->flusher_queue);
  1346. INIT_LIST_HEAD(&wq->flusher_overflow);
  1347. wq->name = name;
  1348. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  1349. INIT_LIST_HEAD(&wq->list);
  1350. cpu_maps_update_begin();
  1351. /*
  1352. * We must initialize cwqs for each possible cpu even if we
  1353. * are going to call destroy_workqueue() finally. Otherwise
  1354. * cpu_up() can hit the uninitialized cwq once we drop the
  1355. * lock.
  1356. */
  1357. for_each_possible_cpu(cpu) {
  1358. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1359. BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
  1360. cwq->cpu = cpu;
  1361. cwq->wq = wq;
  1362. cwq->flush_color = -1;
  1363. cwq->max_active = max_active;
  1364. spin_lock_init(&cwq->lock);
  1365. INIT_LIST_HEAD(&cwq->worklist);
  1366. INIT_LIST_HEAD(&cwq->delayed_works);
  1367. init_waitqueue_head(&cwq->more_work);
  1368. if (failed)
  1369. continue;
  1370. cwq->worker = create_worker(cwq,
  1371. cpu_online(cpu) && !singlethread);
  1372. if (cwq->worker)
  1373. start_worker(cwq->worker);
  1374. else
  1375. failed = true;
  1376. }
  1377. /*
  1378. * workqueue_lock protects global freeze state and workqueues
  1379. * list. Grab it, set max_active accordingly and add the new
  1380. * workqueue to workqueues list.
  1381. */
  1382. spin_lock(&workqueue_lock);
  1383. if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
  1384. for_each_possible_cpu(cpu)
  1385. get_cwq(cpu, wq)->max_active = 0;
  1386. list_add(&wq->list, &workqueues);
  1387. spin_unlock(&workqueue_lock);
  1388. cpu_maps_update_done();
  1389. if (failed) {
  1390. destroy_workqueue(wq);
  1391. wq = NULL;
  1392. }
  1393. return wq;
  1394. err:
  1395. if (wq) {
  1396. free_cwqs(wq->cpu_wq);
  1397. kfree(wq);
  1398. }
  1399. return NULL;
  1400. }
  1401. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  1402. /**
  1403. * destroy_workqueue - safely terminate a workqueue
  1404. * @wq: target workqueue
  1405. *
  1406. * Safely destroy a workqueue. All work currently pending will be done first.
  1407. */
  1408. void destroy_workqueue(struct workqueue_struct *wq)
  1409. {
  1410. int cpu;
  1411. flush_workqueue(wq);
  1412. /*
  1413. * wq list is used to freeze wq, remove from list after
  1414. * flushing is complete in case freeze races us.
  1415. */
  1416. cpu_maps_update_begin();
  1417. spin_lock(&workqueue_lock);
  1418. list_del(&wq->list);
  1419. spin_unlock(&workqueue_lock);
  1420. cpu_maps_update_done();
  1421. for_each_possible_cpu(cpu) {
  1422. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1423. int i;
  1424. if (cwq->worker) {
  1425. destroy_worker(cwq->worker);
  1426. cwq->worker = NULL;
  1427. }
  1428. for (i = 0; i < WORK_NR_COLORS; i++)
  1429. BUG_ON(cwq->nr_in_flight[i]);
  1430. BUG_ON(cwq->nr_active);
  1431. BUG_ON(!list_empty(&cwq->delayed_works));
  1432. }
  1433. free_cwqs(wq->cpu_wq);
  1434. kfree(wq);
  1435. }
  1436. EXPORT_SYMBOL_GPL(destroy_workqueue);
  1437. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  1438. unsigned long action,
  1439. void *hcpu)
  1440. {
  1441. unsigned int cpu = (unsigned long)hcpu;
  1442. struct cpu_workqueue_struct *cwq;
  1443. struct workqueue_struct *wq;
  1444. action &= ~CPU_TASKS_FROZEN;
  1445. list_for_each_entry(wq, &workqueues, list) {
  1446. if (wq->flags & WQ_SINGLE_THREAD)
  1447. continue;
  1448. cwq = get_cwq(cpu, wq);
  1449. switch (action) {
  1450. case CPU_POST_DEAD:
  1451. flush_workqueue(wq);
  1452. break;
  1453. }
  1454. }
  1455. return notifier_from_errno(0);
  1456. }
  1457. #ifdef CONFIG_SMP
  1458. struct work_for_cpu {
  1459. struct completion completion;
  1460. long (*fn)(void *);
  1461. void *arg;
  1462. long ret;
  1463. };
  1464. static int do_work_for_cpu(void *_wfc)
  1465. {
  1466. struct work_for_cpu *wfc = _wfc;
  1467. wfc->ret = wfc->fn(wfc->arg);
  1468. complete(&wfc->completion);
  1469. return 0;
  1470. }
  1471. /**
  1472. * work_on_cpu - run a function in user context on a particular cpu
  1473. * @cpu: the cpu to run on
  1474. * @fn: the function to run
  1475. * @arg: the function arg
  1476. *
  1477. * This will return the value @fn returns.
  1478. * It is up to the caller to ensure that the cpu doesn't go offline.
  1479. * The caller must not hold any locks which would prevent @fn from completing.
  1480. */
  1481. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  1482. {
  1483. struct task_struct *sub_thread;
  1484. struct work_for_cpu wfc = {
  1485. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  1486. .fn = fn,
  1487. .arg = arg,
  1488. };
  1489. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  1490. if (IS_ERR(sub_thread))
  1491. return PTR_ERR(sub_thread);
  1492. kthread_bind(sub_thread, cpu);
  1493. wake_up_process(sub_thread);
  1494. wait_for_completion(&wfc.completion);
  1495. return wfc.ret;
  1496. }
  1497. EXPORT_SYMBOL_GPL(work_on_cpu);
  1498. #endif /* CONFIG_SMP */
  1499. #ifdef CONFIG_FREEZER
  1500. /**
  1501. * freeze_workqueues_begin - begin freezing workqueues
  1502. *
  1503. * Start freezing workqueues. After this function returns, all
  1504. * freezeable workqueues will queue new works to their frozen_works
  1505. * list instead of the cwq ones.
  1506. *
  1507. * CONTEXT:
  1508. * Grabs and releases workqueue_lock and cwq->lock's.
  1509. */
  1510. void freeze_workqueues_begin(void)
  1511. {
  1512. struct workqueue_struct *wq;
  1513. unsigned int cpu;
  1514. spin_lock(&workqueue_lock);
  1515. BUG_ON(workqueue_freezing);
  1516. workqueue_freezing = true;
  1517. for_each_possible_cpu(cpu) {
  1518. list_for_each_entry(wq, &workqueues, list) {
  1519. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1520. spin_lock_irq(&cwq->lock);
  1521. if (wq->flags & WQ_FREEZEABLE)
  1522. cwq->max_active = 0;
  1523. spin_unlock_irq(&cwq->lock);
  1524. }
  1525. }
  1526. spin_unlock(&workqueue_lock);
  1527. }
  1528. /**
  1529. * freeze_workqueues_busy - are freezeable workqueues still busy?
  1530. *
  1531. * Check whether freezing is complete. This function must be called
  1532. * between freeze_workqueues_begin() and thaw_workqueues().
  1533. *
  1534. * CONTEXT:
  1535. * Grabs and releases workqueue_lock.
  1536. *
  1537. * RETURNS:
  1538. * %true if some freezeable workqueues are still busy. %false if
  1539. * freezing is complete.
  1540. */
  1541. bool freeze_workqueues_busy(void)
  1542. {
  1543. struct workqueue_struct *wq;
  1544. unsigned int cpu;
  1545. bool busy = false;
  1546. spin_lock(&workqueue_lock);
  1547. BUG_ON(!workqueue_freezing);
  1548. for_each_possible_cpu(cpu) {
  1549. /*
  1550. * nr_active is monotonically decreasing. It's safe
  1551. * to peek without lock.
  1552. */
  1553. list_for_each_entry(wq, &workqueues, list) {
  1554. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1555. if (!(wq->flags & WQ_FREEZEABLE))
  1556. continue;
  1557. BUG_ON(cwq->nr_active < 0);
  1558. if (cwq->nr_active) {
  1559. busy = true;
  1560. goto out_unlock;
  1561. }
  1562. }
  1563. }
  1564. out_unlock:
  1565. spin_unlock(&workqueue_lock);
  1566. return busy;
  1567. }
  1568. /**
  1569. * thaw_workqueues - thaw workqueues
  1570. *
  1571. * Thaw workqueues. Normal queueing is restored and all collected
  1572. * frozen works are transferred to their respective cwq worklists.
  1573. *
  1574. * CONTEXT:
  1575. * Grabs and releases workqueue_lock and cwq->lock's.
  1576. */
  1577. void thaw_workqueues(void)
  1578. {
  1579. struct workqueue_struct *wq;
  1580. unsigned int cpu;
  1581. spin_lock(&workqueue_lock);
  1582. if (!workqueue_freezing)
  1583. goto out_unlock;
  1584. for_each_possible_cpu(cpu) {
  1585. list_for_each_entry(wq, &workqueues, list) {
  1586. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1587. if (!(wq->flags & WQ_FREEZEABLE))
  1588. continue;
  1589. spin_lock_irq(&cwq->lock);
  1590. /* restore max_active and repopulate worklist */
  1591. cwq->max_active = wq->saved_max_active;
  1592. while (!list_empty(&cwq->delayed_works) &&
  1593. cwq->nr_active < cwq->max_active)
  1594. cwq_activate_first_delayed(cwq);
  1595. wake_up(&cwq->more_work);
  1596. spin_unlock_irq(&cwq->lock);
  1597. }
  1598. }
  1599. workqueue_freezing = false;
  1600. out_unlock:
  1601. spin_unlock(&workqueue_lock);
  1602. }
  1603. #endif /* CONFIG_FREEZER */
  1604. void __init init_workqueues(void)
  1605. {
  1606. unsigned int cpu;
  1607. for_each_possible_cpu(cpu)
  1608. ida_init(&per_cpu(worker_ida, cpu));
  1609. singlethread_cpu = cpumask_first(cpu_possible_mask);
  1610. hotcpu_notifier(workqueue_cpu_callback, 0);
  1611. keventd_wq = create_workqueue("events");
  1612. BUG_ON(!keventd_wq);
  1613. }