workqueue.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  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. enum {
  37. /* global_cwq flags */
  38. GCWQ_FREEZING = 1 << 3, /* freeze in progress */
  39. /* worker flags */
  40. WORKER_STARTED = 1 << 0, /* started */
  41. WORKER_DIE = 1 << 1, /* die die die */
  42. WORKER_IDLE = 1 << 2, /* is idle */
  43. WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
  44. /* gcwq->trustee_state */
  45. TRUSTEE_START = 0, /* start */
  46. TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
  47. TRUSTEE_BUTCHER = 2, /* butcher workers */
  48. TRUSTEE_RELEASE = 3, /* release workers */
  49. TRUSTEE_DONE = 4, /* trustee is done */
  50. BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
  51. BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
  52. BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
  53. TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
  54. };
  55. /*
  56. * Structure fields follow one of the following exclusion rules.
  57. *
  58. * I: Set during initialization and read-only afterwards.
  59. *
  60. * L: gcwq->lock protected. Access with gcwq->lock held.
  61. *
  62. * F: wq->flush_mutex protected.
  63. *
  64. * W: workqueue_lock protected.
  65. */
  66. struct global_cwq;
  67. struct cpu_workqueue_struct;
  68. struct worker {
  69. /* on idle list while idle, on busy hash table while busy */
  70. union {
  71. struct list_head entry; /* L: while idle */
  72. struct hlist_node hentry; /* L: while busy */
  73. };
  74. struct work_struct *current_work; /* L: work being processed */
  75. struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
  76. struct list_head scheduled; /* L: scheduled works */
  77. struct task_struct *task; /* I: worker task */
  78. struct global_cwq *gcwq; /* I: the associated gcwq */
  79. struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
  80. unsigned int flags; /* L: flags */
  81. int id; /* I: worker id */
  82. };
  83. /*
  84. * Global per-cpu workqueue.
  85. */
  86. struct global_cwq {
  87. spinlock_t lock; /* the gcwq lock */
  88. unsigned int cpu; /* I: the associated cpu */
  89. unsigned int flags; /* L: GCWQ_* flags */
  90. int nr_workers; /* L: total number of workers */
  91. int nr_idle; /* L: currently idle ones */
  92. /* workers are chained either in the idle_list or busy_hash */
  93. struct list_head idle_list; /* L: list of idle workers */
  94. struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
  95. /* L: hash of busy workers */
  96. struct ida worker_ida; /* L: for worker IDs */
  97. struct task_struct *trustee; /* L: for gcwq shutdown */
  98. unsigned int trustee_state; /* L: trustee state */
  99. wait_queue_head_t trustee_wait; /* trustee wait */
  100. } ____cacheline_aligned_in_smp;
  101. /*
  102. * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
  103. * work_struct->data are used for flags and thus cwqs need to be
  104. * aligned at two's power of the number of flag bits.
  105. */
  106. struct cpu_workqueue_struct {
  107. struct global_cwq *gcwq; /* I: the associated gcwq */
  108. struct list_head worklist;
  109. struct worker *worker;
  110. struct workqueue_struct *wq; /* I: the owning workqueue */
  111. int work_color; /* L: current color */
  112. int flush_color; /* L: flushing color */
  113. int nr_in_flight[WORK_NR_COLORS];
  114. /* L: nr of in_flight works */
  115. int nr_active; /* L: nr of active works */
  116. int max_active; /* L: max active works */
  117. struct list_head delayed_works; /* L: delayed works */
  118. };
  119. /*
  120. * Structure used to wait for workqueue flush.
  121. */
  122. struct wq_flusher {
  123. struct list_head list; /* F: list of flushers */
  124. int flush_color; /* F: flush color waiting for */
  125. struct completion done; /* flush completion */
  126. };
  127. /*
  128. * The externally visible workqueue abstraction is an array of
  129. * per-CPU workqueues:
  130. */
  131. struct workqueue_struct {
  132. unsigned int flags; /* I: WQ_* flags */
  133. struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
  134. struct list_head list; /* W: list of all workqueues */
  135. struct mutex flush_mutex; /* protects wq flushing */
  136. int work_color; /* F: current work color */
  137. int flush_color; /* F: current flush color */
  138. atomic_t nr_cwqs_to_flush; /* flush in progress */
  139. struct wq_flusher *first_flusher; /* F: first flusher */
  140. struct list_head flusher_queue; /* F: flush waiters */
  141. struct list_head flusher_overflow; /* F: flush overflow list */
  142. unsigned long single_cpu; /* cpu for single cpu wq */
  143. int saved_max_active; /* I: saved cwq max_active */
  144. const char *name; /* I: workqueue name */
  145. #ifdef CONFIG_LOCKDEP
  146. struct lockdep_map lockdep_map;
  147. #endif
  148. };
  149. #define for_each_busy_worker(worker, i, pos, gcwq) \
  150. for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
  151. hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
  152. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  153. static struct debug_obj_descr work_debug_descr;
  154. /*
  155. * fixup_init is called when:
  156. * - an active object is initialized
  157. */
  158. static int work_fixup_init(void *addr, enum debug_obj_state state)
  159. {
  160. struct work_struct *work = addr;
  161. switch (state) {
  162. case ODEBUG_STATE_ACTIVE:
  163. cancel_work_sync(work);
  164. debug_object_init(work, &work_debug_descr);
  165. return 1;
  166. default:
  167. return 0;
  168. }
  169. }
  170. /*
  171. * fixup_activate is called when:
  172. * - an active object is activated
  173. * - an unknown object is activated (might be a statically initialized object)
  174. */
  175. static int work_fixup_activate(void *addr, enum debug_obj_state state)
  176. {
  177. struct work_struct *work = addr;
  178. switch (state) {
  179. case ODEBUG_STATE_NOTAVAILABLE:
  180. /*
  181. * This is not really a fixup. The work struct was
  182. * statically initialized. We just make sure that it
  183. * is tracked in the object tracker.
  184. */
  185. if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
  186. debug_object_init(work, &work_debug_descr);
  187. debug_object_activate(work, &work_debug_descr);
  188. return 0;
  189. }
  190. WARN_ON_ONCE(1);
  191. return 0;
  192. case ODEBUG_STATE_ACTIVE:
  193. WARN_ON(1);
  194. default:
  195. return 0;
  196. }
  197. }
  198. /*
  199. * fixup_free is called when:
  200. * - an active object is freed
  201. */
  202. static int work_fixup_free(void *addr, enum debug_obj_state state)
  203. {
  204. struct work_struct *work = addr;
  205. switch (state) {
  206. case ODEBUG_STATE_ACTIVE:
  207. cancel_work_sync(work);
  208. debug_object_free(work, &work_debug_descr);
  209. return 1;
  210. default:
  211. return 0;
  212. }
  213. }
  214. static struct debug_obj_descr work_debug_descr = {
  215. .name = "work_struct",
  216. .fixup_init = work_fixup_init,
  217. .fixup_activate = work_fixup_activate,
  218. .fixup_free = work_fixup_free,
  219. };
  220. static inline void debug_work_activate(struct work_struct *work)
  221. {
  222. debug_object_activate(work, &work_debug_descr);
  223. }
  224. static inline void debug_work_deactivate(struct work_struct *work)
  225. {
  226. debug_object_deactivate(work, &work_debug_descr);
  227. }
  228. void __init_work(struct work_struct *work, int onstack)
  229. {
  230. if (onstack)
  231. debug_object_init_on_stack(work, &work_debug_descr);
  232. else
  233. debug_object_init(work, &work_debug_descr);
  234. }
  235. EXPORT_SYMBOL_GPL(__init_work);
  236. void destroy_work_on_stack(struct work_struct *work)
  237. {
  238. debug_object_free(work, &work_debug_descr);
  239. }
  240. EXPORT_SYMBOL_GPL(destroy_work_on_stack);
  241. #else
  242. static inline void debug_work_activate(struct work_struct *work) { }
  243. static inline void debug_work_deactivate(struct work_struct *work) { }
  244. #endif
  245. /* Serializes the accesses to the list of workqueues. */
  246. static DEFINE_SPINLOCK(workqueue_lock);
  247. static LIST_HEAD(workqueues);
  248. static bool workqueue_freezing; /* W: have wqs started freezing? */
  249. static DEFINE_PER_CPU(struct global_cwq, global_cwq);
  250. static int worker_thread(void *__worker);
  251. static struct global_cwq *get_gcwq(unsigned int cpu)
  252. {
  253. return &per_cpu(global_cwq, cpu);
  254. }
  255. static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
  256. struct workqueue_struct *wq)
  257. {
  258. return per_cpu_ptr(wq->cpu_wq, cpu);
  259. }
  260. static unsigned int work_color_to_flags(int color)
  261. {
  262. return color << WORK_STRUCT_COLOR_SHIFT;
  263. }
  264. static int get_work_color(struct work_struct *work)
  265. {
  266. return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
  267. ((1 << WORK_STRUCT_COLOR_BITS) - 1);
  268. }
  269. static int work_next_color(int color)
  270. {
  271. return (color + 1) % WORK_NR_COLORS;
  272. }
  273. /*
  274. * Work data points to the cwq while a work is on queue. Once
  275. * execution starts, it points to the cpu the work was last on. This
  276. * can be distinguished by comparing the data value against
  277. * PAGE_OFFSET.
  278. *
  279. * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
  280. * cwq, cpu or clear work->data. These functions should only be
  281. * called while the work is owned - ie. while the PENDING bit is set.
  282. *
  283. * get_work_[g]cwq() can be used to obtain the gcwq or cwq
  284. * corresponding to a work. gcwq is available once the work has been
  285. * queued anywhere after initialization. cwq is available only from
  286. * queueing until execution starts.
  287. */
  288. static inline void set_work_data(struct work_struct *work, unsigned long data,
  289. unsigned long flags)
  290. {
  291. BUG_ON(!work_pending(work));
  292. atomic_long_set(&work->data, data | flags | work_static(work));
  293. }
  294. static void set_work_cwq(struct work_struct *work,
  295. struct cpu_workqueue_struct *cwq,
  296. unsigned long extra_flags)
  297. {
  298. set_work_data(work, (unsigned long)cwq,
  299. WORK_STRUCT_PENDING | extra_flags);
  300. }
  301. static void set_work_cpu(struct work_struct *work, unsigned int cpu)
  302. {
  303. set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
  304. }
  305. static void clear_work_data(struct work_struct *work)
  306. {
  307. set_work_data(work, WORK_STRUCT_NO_CPU, 0);
  308. }
  309. static inline unsigned long get_work_data(struct work_struct *work)
  310. {
  311. return atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK;
  312. }
  313. static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
  314. {
  315. unsigned long data = get_work_data(work);
  316. return data >= PAGE_OFFSET ? (void *)data : NULL;
  317. }
  318. static struct global_cwq *get_work_gcwq(struct work_struct *work)
  319. {
  320. unsigned long data = get_work_data(work);
  321. unsigned int cpu;
  322. if (data >= PAGE_OFFSET)
  323. return ((struct cpu_workqueue_struct *)data)->gcwq;
  324. cpu = data >> WORK_STRUCT_FLAG_BITS;
  325. if (cpu == NR_CPUS)
  326. return NULL;
  327. BUG_ON(cpu >= num_possible_cpus());
  328. return get_gcwq(cpu);
  329. }
  330. /**
  331. * busy_worker_head - return the busy hash head for a work
  332. * @gcwq: gcwq of interest
  333. * @work: work to be hashed
  334. *
  335. * Return hash head of @gcwq for @work.
  336. *
  337. * CONTEXT:
  338. * spin_lock_irq(gcwq->lock).
  339. *
  340. * RETURNS:
  341. * Pointer to the hash head.
  342. */
  343. static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
  344. struct work_struct *work)
  345. {
  346. const int base_shift = ilog2(sizeof(struct work_struct));
  347. unsigned long v = (unsigned long)work;
  348. /* simple shift and fold hash, do we need something better? */
  349. v >>= base_shift;
  350. v += v >> BUSY_WORKER_HASH_ORDER;
  351. v &= BUSY_WORKER_HASH_MASK;
  352. return &gcwq->busy_hash[v];
  353. }
  354. /**
  355. * __find_worker_executing_work - find worker which is executing a work
  356. * @gcwq: gcwq of interest
  357. * @bwh: hash head as returned by busy_worker_head()
  358. * @work: work to find worker for
  359. *
  360. * Find a worker which is executing @work on @gcwq. @bwh should be
  361. * the hash head obtained by calling busy_worker_head() with the same
  362. * work.
  363. *
  364. * CONTEXT:
  365. * spin_lock_irq(gcwq->lock).
  366. *
  367. * RETURNS:
  368. * Pointer to worker which is executing @work if found, NULL
  369. * otherwise.
  370. */
  371. static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
  372. struct hlist_head *bwh,
  373. struct work_struct *work)
  374. {
  375. struct worker *worker;
  376. struct hlist_node *tmp;
  377. hlist_for_each_entry(worker, tmp, bwh, hentry)
  378. if (worker->current_work == work)
  379. return worker;
  380. return NULL;
  381. }
  382. /**
  383. * find_worker_executing_work - find worker which is executing a work
  384. * @gcwq: gcwq of interest
  385. * @work: work to find worker for
  386. *
  387. * Find a worker which is executing @work on @gcwq. This function is
  388. * identical to __find_worker_executing_work() except that this
  389. * function calculates @bwh itself.
  390. *
  391. * CONTEXT:
  392. * spin_lock_irq(gcwq->lock).
  393. *
  394. * RETURNS:
  395. * Pointer to worker which is executing @work if found, NULL
  396. * otherwise.
  397. */
  398. static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
  399. struct work_struct *work)
  400. {
  401. return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
  402. work);
  403. }
  404. /**
  405. * insert_work - insert a work into cwq
  406. * @cwq: cwq @work belongs to
  407. * @work: work to insert
  408. * @head: insertion point
  409. * @extra_flags: extra WORK_STRUCT_* flags to set
  410. *
  411. * Insert @work into @cwq after @head.
  412. *
  413. * CONTEXT:
  414. * spin_lock_irq(gcwq->lock).
  415. */
  416. static void insert_work(struct cpu_workqueue_struct *cwq,
  417. struct work_struct *work, struct list_head *head,
  418. unsigned int extra_flags)
  419. {
  420. /* we own @work, set data and link */
  421. set_work_cwq(work, cwq, extra_flags);
  422. /*
  423. * Ensure that we get the right work->data if we see the
  424. * result of list_add() below, see try_to_grab_pending().
  425. */
  426. smp_wmb();
  427. list_add_tail(&work->entry, head);
  428. wake_up_process(cwq->worker->task);
  429. }
  430. /**
  431. * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing
  432. * @cwq: cwq to unbind
  433. *
  434. * Try to unbind @cwq from single cpu workqueue processing. If
  435. * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed.
  436. *
  437. * CONTEXT:
  438. * spin_lock_irq(gcwq->lock).
  439. */
  440. static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq)
  441. {
  442. struct workqueue_struct *wq = cwq->wq;
  443. struct global_cwq *gcwq = cwq->gcwq;
  444. BUG_ON(wq->single_cpu != gcwq->cpu);
  445. /*
  446. * Unbind from workqueue if @cwq is not frozen. If frozen,
  447. * thaw_workqueues() will either restart processing on this
  448. * cpu or unbind if empty. This keeps works queued while
  449. * frozen fully ordered and flushable.
  450. */
  451. if (likely(!(gcwq->flags & GCWQ_FREEZING))) {
  452. smp_wmb(); /* paired with cmpxchg() in __queue_work() */
  453. wq->single_cpu = NR_CPUS;
  454. }
  455. }
  456. static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
  457. struct work_struct *work)
  458. {
  459. struct global_cwq *gcwq;
  460. struct cpu_workqueue_struct *cwq;
  461. struct list_head *worklist;
  462. unsigned long flags;
  463. bool arbitrate;
  464. debug_work_activate(work);
  465. /*
  466. * Determine gcwq to use. SINGLE_CPU is inherently
  467. * NON_REENTRANT, so test it first.
  468. */
  469. if (!(wq->flags & WQ_SINGLE_CPU)) {
  470. struct global_cwq *last_gcwq;
  471. /*
  472. * It's multi cpu. If @wq is non-reentrant and @work
  473. * was previously on a different cpu, it might still
  474. * be running there, in which case the work needs to
  475. * be queued on that cpu to guarantee non-reentrance.
  476. */
  477. gcwq = get_gcwq(cpu);
  478. if (wq->flags & WQ_NON_REENTRANT &&
  479. (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
  480. struct worker *worker;
  481. spin_lock_irqsave(&last_gcwq->lock, flags);
  482. worker = find_worker_executing_work(last_gcwq, work);
  483. if (worker && worker->current_cwq->wq == wq)
  484. gcwq = last_gcwq;
  485. else {
  486. /* meh... not running there, queue here */
  487. spin_unlock_irqrestore(&last_gcwq->lock, flags);
  488. spin_lock_irqsave(&gcwq->lock, flags);
  489. }
  490. } else
  491. spin_lock_irqsave(&gcwq->lock, flags);
  492. } else {
  493. unsigned int req_cpu = cpu;
  494. /*
  495. * It's a bit more complex for single cpu workqueues.
  496. * We first need to determine which cpu is going to be
  497. * used. If no cpu is currently serving this
  498. * workqueue, arbitrate using atomic accesses to
  499. * wq->single_cpu; otherwise, use the current one.
  500. */
  501. retry:
  502. cpu = wq->single_cpu;
  503. arbitrate = cpu == NR_CPUS;
  504. if (arbitrate)
  505. cpu = req_cpu;
  506. gcwq = get_gcwq(cpu);
  507. spin_lock_irqsave(&gcwq->lock, flags);
  508. /*
  509. * The following cmpxchg() is a full barrier paired
  510. * with smp_wmb() in cwq_unbind_single_cpu() and
  511. * guarantees that all changes to wq->st_* fields are
  512. * visible on the new cpu after this point.
  513. */
  514. if (arbitrate)
  515. cmpxchg(&wq->single_cpu, NR_CPUS, cpu);
  516. if (unlikely(wq->single_cpu != cpu)) {
  517. spin_unlock_irqrestore(&gcwq->lock, flags);
  518. goto retry;
  519. }
  520. }
  521. /* gcwq determined, get cwq and queue */
  522. cwq = get_cwq(gcwq->cpu, wq);
  523. BUG_ON(!list_empty(&work->entry));
  524. cwq->nr_in_flight[cwq->work_color]++;
  525. if (likely(cwq->nr_active < cwq->max_active)) {
  526. cwq->nr_active++;
  527. worklist = &cwq->worklist;
  528. } else
  529. worklist = &cwq->delayed_works;
  530. insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
  531. spin_unlock_irqrestore(&gcwq->lock, flags);
  532. }
  533. /**
  534. * queue_work - queue work on a workqueue
  535. * @wq: workqueue to use
  536. * @work: work to queue
  537. *
  538. * Returns 0 if @work was already on a queue, non-zero otherwise.
  539. *
  540. * We queue the work to the CPU on which it was submitted, but if the CPU dies
  541. * it can be processed by another CPU.
  542. */
  543. int queue_work(struct workqueue_struct *wq, struct work_struct *work)
  544. {
  545. int ret;
  546. ret = queue_work_on(get_cpu(), wq, work);
  547. put_cpu();
  548. return ret;
  549. }
  550. EXPORT_SYMBOL_GPL(queue_work);
  551. /**
  552. * queue_work_on - queue work on specific cpu
  553. * @cpu: CPU number to execute work on
  554. * @wq: workqueue to use
  555. * @work: work to queue
  556. *
  557. * Returns 0 if @work was already on a queue, non-zero otherwise.
  558. *
  559. * We queue the work to a specific CPU, the caller must ensure it
  560. * can't go away.
  561. */
  562. int
  563. queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
  564. {
  565. int ret = 0;
  566. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  567. __queue_work(cpu, wq, work);
  568. ret = 1;
  569. }
  570. return ret;
  571. }
  572. EXPORT_SYMBOL_GPL(queue_work_on);
  573. static void delayed_work_timer_fn(unsigned long __data)
  574. {
  575. struct delayed_work *dwork = (struct delayed_work *)__data;
  576. struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
  577. __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
  578. }
  579. /**
  580. * queue_delayed_work - queue work on a workqueue after delay
  581. * @wq: workqueue to use
  582. * @dwork: delayable work to queue
  583. * @delay: number of jiffies to wait before queueing
  584. *
  585. * Returns 0 if @work was already on a queue, non-zero otherwise.
  586. */
  587. int queue_delayed_work(struct workqueue_struct *wq,
  588. struct delayed_work *dwork, unsigned long delay)
  589. {
  590. if (delay == 0)
  591. return queue_work(wq, &dwork->work);
  592. return queue_delayed_work_on(-1, wq, dwork, delay);
  593. }
  594. EXPORT_SYMBOL_GPL(queue_delayed_work);
  595. /**
  596. * queue_delayed_work_on - queue work on specific CPU after delay
  597. * @cpu: CPU number to execute work on
  598. * @wq: workqueue to use
  599. * @dwork: work to queue
  600. * @delay: number of jiffies to wait before queueing
  601. *
  602. * Returns 0 if @work was already on a queue, non-zero otherwise.
  603. */
  604. int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  605. struct delayed_work *dwork, unsigned long delay)
  606. {
  607. int ret = 0;
  608. struct timer_list *timer = &dwork->timer;
  609. struct work_struct *work = &dwork->work;
  610. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  611. struct global_cwq *gcwq = get_work_gcwq(work);
  612. unsigned int lcpu = gcwq ? gcwq->cpu : raw_smp_processor_id();
  613. BUG_ON(timer_pending(timer));
  614. BUG_ON(!list_empty(&work->entry));
  615. timer_stats_timer_set_start_info(&dwork->timer);
  616. /*
  617. * This stores cwq for the moment, for the timer_fn.
  618. * Note that the work's gcwq is preserved to allow
  619. * reentrance detection for delayed works.
  620. */
  621. set_work_cwq(work, get_cwq(lcpu, wq), 0);
  622. timer->expires = jiffies + delay;
  623. timer->data = (unsigned long)dwork;
  624. timer->function = delayed_work_timer_fn;
  625. if (unlikely(cpu >= 0))
  626. add_timer_on(timer, cpu);
  627. else
  628. add_timer(timer);
  629. ret = 1;
  630. }
  631. return ret;
  632. }
  633. EXPORT_SYMBOL_GPL(queue_delayed_work_on);
  634. /**
  635. * worker_enter_idle - enter idle state
  636. * @worker: worker which is entering idle state
  637. *
  638. * @worker is entering idle state. Update stats and idle timer if
  639. * necessary.
  640. *
  641. * LOCKING:
  642. * spin_lock_irq(gcwq->lock).
  643. */
  644. static void worker_enter_idle(struct worker *worker)
  645. {
  646. struct global_cwq *gcwq = worker->gcwq;
  647. BUG_ON(worker->flags & WORKER_IDLE);
  648. BUG_ON(!list_empty(&worker->entry) &&
  649. (worker->hentry.next || worker->hentry.pprev));
  650. worker->flags |= WORKER_IDLE;
  651. gcwq->nr_idle++;
  652. /* idle_list is LIFO */
  653. list_add(&worker->entry, &gcwq->idle_list);
  654. if (unlikely(worker->flags & WORKER_ROGUE))
  655. wake_up_all(&gcwq->trustee_wait);
  656. }
  657. /**
  658. * worker_leave_idle - leave idle state
  659. * @worker: worker which is leaving idle state
  660. *
  661. * @worker is leaving idle state. Update stats.
  662. *
  663. * LOCKING:
  664. * spin_lock_irq(gcwq->lock).
  665. */
  666. static void worker_leave_idle(struct worker *worker)
  667. {
  668. struct global_cwq *gcwq = worker->gcwq;
  669. BUG_ON(!(worker->flags & WORKER_IDLE));
  670. worker->flags &= ~WORKER_IDLE;
  671. gcwq->nr_idle--;
  672. list_del_init(&worker->entry);
  673. }
  674. static struct worker *alloc_worker(void)
  675. {
  676. struct worker *worker;
  677. worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  678. if (worker) {
  679. INIT_LIST_HEAD(&worker->entry);
  680. INIT_LIST_HEAD(&worker->scheduled);
  681. }
  682. return worker;
  683. }
  684. /**
  685. * create_worker - create a new workqueue worker
  686. * @cwq: cwq the new worker will belong to
  687. * @bind: whether to set affinity to @cpu or not
  688. *
  689. * Create a new worker which is bound to @cwq. The returned worker
  690. * can be started by calling start_worker() or destroyed using
  691. * destroy_worker().
  692. *
  693. * CONTEXT:
  694. * Might sleep. Does GFP_KERNEL allocations.
  695. *
  696. * RETURNS:
  697. * Pointer to the newly created worker.
  698. */
  699. static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
  700. {
  701. struct global_cwq *gcwq = cwq->gcwq;
  702. int id = -1;
  703. struct worker *worker = NULL;
  704. spin_lock_irq(&gcwq->lock);
  705. while (ida_get_new(&gcwq->worker_ida, &id)) {
  706. spin_unlock_irq(&gcwq->lock);
  707. if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
  708. goto fail;
  709. spin_lock_irq(&gcwq->lock);
  710. }
  711. spin_unlock_irq(&gcwq->lock);
  712. worker = alloc_worker();
  713. if (!worker)
  714. goto fail;
  715. worker->gcwq = gcwq;
  716. worker->cwq = cwq;
  717. worker->id = id;
  718. worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
  719. gcwq->cpu, id);
  720. if (IS_ERR(worker->task))
  721. goto fail;
  722. /*
  723. * A rogue worker will become a regular one if CPU comes
  724. * online later on. Make sure every worker has
  725. * PF_THREAD_BOUND set.
  726. */
  727. if (bind)
  728. kthread_bind(worker->task, gcwq->cpu);
  729. else
  730. worker->task->flags |= PF_THREAD_BOUND;
  731. return worker;
  732. fail:
  733. if (id >= 0) {
  734. spin_lock_irq(&gcwq->lock);
  735. ida_remove(&gcwq->worker_ida, id);
  736. spin_unlock_irq(&gcwq->lock);
  737. }
  738. kfree(worker);
  739. return NULL;
  740. }
  741. /**
  742. * start_worker - start a newly created worker
  743. * @worker: worker to start
  744. *
  745. * Make the gcwq aware of @worker and start it.
  746. *
  747. * CONTEXT:
  748. * spin_lock_irq(gcwq->lock).
  749. */
  750. static void start_worker(struct worker *worker)
  751. {
  752. worker->flags |= WORKER_STARTED;
  753. worker->gcwq->nr_workers++;
  754. worker_enter_idle(worker);
  755. wake_up_process(worker->task);
  756. }
  757. /**
  758. * destroy_worker - destroy a workqueue worker
  759. * @worker: worker to be destroyed
  760. *
  761. * Destroy @worker and adjust @gcwq stats accordingly.
  762. *
  763. * CONTEXT:
  764. * spin_lock_irq(gcwq->lock) which is released and regrabbed.
  765. */
  766. static void destroy_worker(struct worker *worker)
  767. {
  768. struct global_cwq *gcwq = worker->gcwq;
  769. int id = worker->id;
  770. /* sanity check frenzy */
  771. BUG_ON(worker->current_work);
  772. BUG_ON(!list_empty(&worker->scheduled));
  773. if (worker->flags & WORKER_STARTED)
  774. gcwq->nr_workers--;
  775. if (worker->flags & WORKER_IDLE)
  776. gcwq->nr_idle--;
  777. list_del_init(&worker->entry);
  778. worker->flags |= WORKER_DIE;
  779. spin_unlock_irq(&gcwq->lock);
  780. kthread_stop(worker->task);
  781. kfree(worker);
  782. spin_lock_irq(&gcwq->lock);
  783. ida_remove(&gcwq->worker_ida, id);
  784. }
  785. /**
  786. * move_linked_works - move linked works to a list
  787. * @work: start of series of works to be scheduled
  788. * @head: target list to append @work to
  789. * @nextp: out paramter for nested worklist walking
  790. *
  791. * Schedule linked works starting from @work to @head. Work series to
  792. * be scheduled starts at @work and includes any consecutive work with
  793. * WORK_STRUCT_LINKED set in its predecessor.
  794. *
  795. * If @nextp is not NULL, it's updated to point to the next work of
  796. * the last scheduled work. This allows move_linked_works() to be
  797. * nested inside outer list_for_each_entry_safe().
  798. *
  799. * CONTEXT:
  800. * spin_lock_irq(gcwq->lock).
  801. */
  802. static void move_linked_works(struct work_struct *work, struct list_head *head,
  803. struct work_struct **nextp)
  804. {
  805. struct work_struct *n;
  806. /*
  807. * Linked worklist will always end before the end of the list,
  808. * use NULL for list head.
  809. */
  810. list_for_each_entry_safe_from(work, n, NULL, entry) {
  811. list_move_tail(&work->entry, head);
  812. if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
  813. break;
  814. }
  815. /*
  816. * If we're already inside safe list traversal and have moved
  817. * multiple works to the scheduled queue, the next position
  818. * needs to be updated.
  819. */
  820. if (nextp)
  821. *nextp = n;
  822. }
  823. static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
  824. {
  825. struct work_struct *work = list_first_entry(&cwq->delayed_works,
  826. struct work_struct, entry);
  827. move_linked_works(work, &cwq->worklist, NULL);
  828. cwq->nr_active++;
  829. }
  830. /**
  831. * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
  832. * @cwq: cwq of interest
  833. * @color: color of work which left the queue
  834. *
  835. * A work either has completed or is removed from pending queue,
  836. * decrement nr_in_flight of its cwq and handle workqueue flushing.
  837. *
  838. * CONTEXT:
  839. * spin_lock_irq(gcwq->lock).
  840. */
  841. static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
  842. {
  843. /* ignore uncolored works */
  844. if (color == WORK_NO_COLOR)
  845. return;
  846. cwq->nr_in_flight[color]--;
  847. cwq->nr_active--;
  848. if (!list_empty(&cwq->delayed_works)) {
  849. /* one down, submit a delayed one */
  850. if (cwq->nr_active < cwq->max_active)
  851. cwq_activate_first_delayed(cwq);
  852. } else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) {
  853. /* this was the last work, unbind from single cpu */
  854. cwq_unbind_single_cpu(cwq);
  855. }
  856. /* is flush in progress and are we at the flushing tip? */
  857. if (likely(cwq->flush_color != color))
  858. return;
  859. /* are there still in-flight works? */
  860. if (cwq->nr_in_flight[color])
  861. return;
  862. /* this cwq is done, clear flush_color */
  863. cwq->flush_color = -1;
  864. /*
  865. * If this was the last cwq, wake up the first flusher. It
  866. * will handle the rest.
  867. */
  868. if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
  869. complete(&cwq->wq->first_flusher->done);
  870. }
  871. /**
  872. * process_one_work - process single work
  873. * @worker: self
  874. * @work: work to process
  875. *
  876. * Process @work. This function contains all the logics necessary to
  877. * process a single work including synchronization against and
  878. * interaction with other workers on the same cpu, queueing and
  879. * flushing. As long as context requirement is met, any worker can
  880. * call this function to process a work.
  881. *
  882. * CONTEXT:
  883. * spin_lock_irq(gcwq->lock) which is released and regrabbed.
  884. */
  885. static void process_one_work(struct worker *worker, struct work_struct *work)
  886. {
  887. struct cpu_workqueue_struct *cwq = worker->cwq;
  888. struct global_cwq *gcwq = cwq->gcwq;
  889. struct hlist_head *bwh = busy_worker_head(gcwq, work);
  890. work_func_t f = work->func;
  891. int work_color;
  892. #ifdef CONFIG_LOCKDEP
  893. /*
  894. * It is permissible to free the struct work_struct from
  895. * inside the function that is called from it, this we need to
  896. * take into account for lockdep too. To avoid bogus "held
  897. * lock freed" warnings as well as problems when looking into
  898. * work->lockdep_map, make a copy and use that here.
  899. */
  900. struct lockdep_map lockdep_map = work->lockdep_map;
  901. #endif
  902. /* claim and process */
  903. debug_work_deactivate(work);
  904. hlist_add_head(&worker->hentry, bwh);
  905. worker->current_work = work;
  906. worker->current_cwq = cwq;
  907. work_color = get_work_color(work);
  908. BUG_ON(get_work_cwq(work) != cwq);
  909. /* record the current cpu number in the work data and dequeue */
  910. set_work_cpu(work, gcwq->cpu);
  911. list_del_init(&work->entry);
  912. spin_unlock_irq(&gcwq->lock);
  913. work_clear_pending(work);
  914. lock_map_acquire(&cwq->wq->lockdep_map);
  915. lock_map_acquire(&lockdep_map);
  916. f(work);
  917. lock_map_release(&lockdep_map);
  918. lock_map_release(&cwq->wq->lockdep_map);
  919. if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
  920. printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
  921. "%s/0x%08x/%d\n",
  922. current->comm, preempt_count(), task_pid_nr(current));
  923. printk(KERN_ERR " last function: ");
  924. print_symbol("%s\n", (unsigned long)f);
  925. debug_show_held_locks(current);
  926. dump_stack();
  927. }
  928. spin_lock_irq(&gcwq->lock);
  929. /* we're done with it, release */
  930. hlist_del_init(&worker->hentry);
  931. worker->current_work = NULL;
  932. worker->current_cwq = NULL;
  933. cwq_dec_nr_in_flight(cwq, work_color);
  934. }
  935. /**
  936. * process_scheduled_works - process scheduled works
  937. * @worker: self
  938. *
  939. * Process all scheduled works. Please note that the scheduled list
  940. * may change while processing a work, so this function repeatedly
  941. * fetches a work from the top and executes it.
  942. *
  943. * CONTEXT:
  944. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  945. * multiple times.
  946. */
  947. static void process_scheduled_works(struct worker *worker)
  948. {
  949. while (!list_empty(&worker->scheduled)) {
  950. struct work_struct *work = list_first_entry(&worker->scheduled,
  951. struct work_struct, entry);
  952. process_one_work(worker, work);
  953. }
  954. }
  955. /**
  956. * worker_thread - the worker thread function
  957. * @__worker: self
  958. *
  959. * The cwq worker thread function.
  960. */
  961. static int worker_thread(void *__worker)
  962. {
  963. struct worker *worker = __worker;
  964. struct global_cwq *gcwq = worker->gcwq;
  965. struct cpu_workqueue_struct *cwq = worker->cwq;
  966. woke_up:
  967. spin_lock_irq(&gcwq->lock);
  968. /* DIE can be set only while we're idle, checking here is enough */
  969. if (worker->flags & WORKER_DIE) {
  970. spin_unlock_irq(&gcwq->lock);
  971. return 0;
  972. }
  973. worker_leave_idle(worker);
  974. recheck:
  975. /*
  976. * ->scheduled list can only be filled while a worker is
  977. * preparing to process a work or actually processing it.
  978. * Make sure nobody diddled with it while I was sleeping.
  979. */
  980. BUG_ON(!list_empty(&worker->scheduled));
  981. while (!list_empty(&cwq->worklist)) {
  982. struct work_struct *work =
  983. list_first_entry(&cwq->worklist,
  984. struct work_struct, entry);
  985. /*
  986. * The following is a rather inefficient way to close
  987. * race window against cpu hotplug operations. Will
  988. * be replaced soon.
  989. */
  990. if (unlikely(!(worker->flags & WORKER_ROGUE) &&
  991. !cpumask_equal(&worker->task->cpus_allowed,
  992. get_cpu_mask(gcwq->cpu)))) {
  993. spin_unlock_irq(&gcwq->lock);
  994. set_cpus_allowed_ptr(worker->task,
  995. get_cpu_mask(gcwq->cpu));
  996. cpu_relax();
  997. spin_lock_irq(&gcwq->lock);
  998. goto recheck;
  999. }
  1000. if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
  1001. /* optimization path, not strictly necessary */
  1002. process_one_work(worker, work);
  1003. if (unlikely(!list_empty(&worker->scheduled)))
  1004. process_scheduled_works(worker);
  1005. } else {
  1006. move_linked_works(work, &worker->scheduled, NULL);
  1007. process_scheduled_works(worker);
  1008. }
  1009. }
  1010. /*
  1011. * gcwq->lock is held and there's no work to process, sleep.
  1012. * Workers are woken up only while holding gcwq->lock, so
  1013. * setting the current state before releasing gcwq->lock is
  1014. * enough to prevent losing any event.
  1015. */
  1016. worker_enter_idle(worker);
  1017. __set_current_state(TASK_INTERRUPTIBLE);
  1018. spin_unlock_irq(&gcwq->lock);
  1019. schedule();
  1020. goto woke_up;
  1021. }
  1022. struct wq_barrier {
  1023. struct work_struct work;
  1024. struct completion done;
  1025. };
  1026. static void wq_barrier_func(struct work_struct *work)
  1027. {
  1028. struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
  1029. complete(&barr->done);
  1030. }
  1031. /**
  1032. * insert_wq_barrier - insert a barrier work
  1033. * @cwq: cwq to insert barrier into
  1034. * @barr: wq_barrier to insert
  1035. * @target: target work to attach @barr to
  1036. * @worker: worker currently executing @target, NULL if @target is not executing
  1037. *
  1038. * @barr is linked to @target such that @barr is completed only after
  1039. * @target finishes execution. Please note that the ordering
  1040. * guarantee is observed only with respect to @target and on the local
  1041. * cpu.
  1042. *
  1043. * Currently, a queued barrier can't be canceled. This is because
  1044. * try_to_grab_pending() can't determine whether the work to be
  1045. * grabbed is at the head of the queue and thus can't clear LINKED
  1046. * flag of the previous work while there must be a valid next work
  1047. * after a work with LINKED flag set.
  1048. *
  1049. * Note that when @worker is non-NULL, @target may be modified
  1050. * underneath us, so we can't reliably determine cwq from @target.
  1051. *
  1052. * CONTEXT:
  1053. * spin_lock_irq(gcwq->lock).
  1054. */
  1055. static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
  1056. struct wq_barrier *barr,
  1057. struct work_struct *target, struct worker *worker)
  1058. {
  1059. struct list_head *head;
  1060. unsigned int linked = 0;
  1061. /*
  1062. * debugobject calls are safe here even with gcwq->lock locked
  1063. * as we know for sure that this will not trigger any of the
  1064. * checks and call back into the fixup functions where we
  1065. * might deadlock.
  1066. */
  1067. INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
  1068. __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
  1069. init_completion(&barr->done);
  1070. /*
  1071. * If @target is currently being executed, schedule the
  1072. * barrier to the worker; otherwise, put it after @target.
  1073. */
  1074. if (worker)
  1075. head = worker->scheduled.next;
  1076. else {
  1077. unsigned long *bits = work_data_bits(target);
  1078. head = target->entry.next;
  1079. /* there can already be other linked works, inherit and set */
  1080. linked = *bits & WORK_STRUCT_LINKED;
  1081. __set_bit(WORK_STRUCT_LINKED_BIT, bits);
  1082. }
  1083. debug_work_activate(&barr->work);
  1084. insert_work(cwq, &barr->work, head,
  1085. work_color_to_flags(WORK_NO_COLOR) | linked);
  1086. }
  1087. /**
  1088. * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
  1089. * @wq: workqueue being flushed
  1090. * @flush_color: new flush color, < 0 for no-op
  1091. * @work_color: new work color, < 0 for no-op
  1092. *
  1093. * Prepare cwqs for workqueue flushing.
  1094. *
  1095. * If @flush_color is non-negative, flush_color on all cwqs should be
  1096. * -1. If no cwq has in-flight commands at the specified color, all
  1097. * cwq->flush_color's stay at -1 and %false is returned. If any cwq
  1098. * has in flight commands, its cwq->flush_color is set to
  1099. * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
  1100. * wakeup logic is armed and %true is returned.
  1101. *
  1102. * The caller should have initialized @wq->first_flusher prior to
  1103. * calling this function with non-negative @flush_color. If
  1104. * @flush_color is negative, no flush color update is done and %false
  1105. * is returned.
  1106. *
  1107. * If @work_color is non-negative, all cwqs should have the same
  1108. * work_color which is previous to @work_color and all will be
  1109. * advanced to @work_color.
  1110. *
  1111. * CONTEXT:
  1112. * mutex_lock(wq->flush_mutex).
  1113. *
  1114. * RETURNS:
  1115. * %true if @flush_color >= 0 and there's something to flush. %false
  1116. * otherwise.
  1117. */
  1118. static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
  1119. int flush_color, int work_color)
  1120. {
  1121. bool wait = false;
  1122. unsigned int cpu;
  1123. if (flush_color >= 0) {
  1124. BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
  1125. atomic_set(&wq->nr_cwqs_to_flush, 1);
  1126. }
  1127. for_each_possible_cpu(cpu) {
  1128. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1129. struct global_cwq *gcwq = cwq->gcwq;
  1130. spin_lock_irq(&gcwq->lock);
  1131. if (flush_color >= 0) {
  1132. BUG_ON(cwq->flush_color != -1);
  1133. if (cwq->nr_in_flight[flush_color]) {
  1134. cwq->flush_color = flush_color;
  1135. atomic_inc(&wq->nr_cwqs_to_flush);
  1136. wait = true;
  1137. }
  1138. }
  1139. if (work_color >= 0) {
  1140. BUG_ON(work_color != work_next_color(cwq->work_color));
  1141. cwq->work_color = work_color;
  1142. }
  1143. spin_unlock_irq(&gcwq->lock);
  1144. }
  1145. if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
  1146. complete(&wq->first_flusher->done);
  1147. return wait;
  1148. }
  1149. /**
  1150. * flush_workqueue - ensure that any scheduled work has run to completion.
  1151. * @wq: workqueue to flush
  1152. *
  1153. * Forces execution of the workqueue and blocks until its completion.
  1154. * This is typically used in driver shutdown handlers.
  1155. *
  1156. * We sleep until all works which were queued on entry have been handled,
  1157. * but we are not livelocked by new incoming ones.
  1158. */
  1159. void flush_workqueue(struct workqueue_struct *wq)
  1160. {
  1161. struct wq_flusher this_flusher = {
  1162. .list = LIST_HEAD_INIT(this_flusher.list),
  1163. .flush_color = -1,
  1164. .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
  1165. };
  1166. int next_color;
  1167. lock_map_acquire(&wq->lockdep_map);
  1168. lock_map_release(&wq->lockdep_map);
  1169. mutex_lock(&wq->flush_mutex);
  1170. /*
  1171. * Start-to-wait phase
  1172. */
  1173. next_color = work_next_color(wq->work_color);
  1174. if (next_color != wq->flush_color) {
  1175. /*
  1176. * Color space is not full. The current work_color
  1177. * becomes our flush_color and work_color is advanced
  1178. * by one.
  1179. */
  1180. BUG_ON(!list_empty(&wq->flusher_overflow));
  1181. this_flusher.flush_color = wq->work_color;
  1182. wq->work_color = next_color;
  1183. if (!wq->first_flusher) {
  1184. /* no flush in progress, become the first flusher */
  1185. BUG_ON(wq->flush_color != this_flusher.flush_color);
  1186. wq->first_flusher = &this_flusher;
  1187. if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
  1188. wq->work_color)) {
  1189. /* nothing to flush, done */
  1190. wq->flush_color = next_color;
  1191. wq->first_flusher = NULL;
  1192. goto out_unlock;
  1193. }
  1194. } else {
  1195. /* wait in queue */
  1196. BUG_ON(wq->flush_color == this_flusher.flush_color);
  1197. list_add_tail(&this_flusher.list, &wq->flusher_queue);
  1198. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  1199. }
  1200. } else {
  1201. /*
  1202. * Oops, color space is full, wait on overflow queue.
  1203. * The next flush completion will assign us
  1204. * flush_color and transfer to flusher_queue.
  1205. */
  1206. list_add_tail(&this_flusher.list, &wq->flusher_overflow);
  1207. }
  1208. mutex_unlock(&wq->flush_mutex);
  1209. wait_for_completion(&this_flusher.done);
  1210. /*
  1211. * Wake-up-and-cascade phase
  1212. *
  1213. * First flushers are responsible for cascading flushes and
  1214. * handling overflow. Non-first flushers can simply return.
  1215. */
  1216. if (wq->first_flusher != &this_flusher)
  1217. return;
  1218. mutex_lock(&wq->flush_mutex);
  1219. wq->first_flusher = NULL;
  1220. BUG_ON(!list_empty(&this_flusher.list));
  1221. BUG_ON(wq->flush_color != this_flusher.flush_color);
  1222. while (true) {
  1223. struct wq_flusher *next, *tmp;
  1224. /* complete all the flushers sharing the current flush color */
  1225. list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
  1226. if (next->flush_color != wq->flush_color)
  1227. break;
  1228. list_del_init(&next->list);
  1229. complete(&next->done);
  1230. }
  1231. BUG_ON(!list_empty(&wq->flusher_overflow) &&
  1232. wq->flush_color != work_next_color(wq->work_color));
  1233. /* this flush_color is finished, advance by one */
  1234. wq->flush_color = work_next_color(wq->flush_color);
  1235. /* one color has been freed, handle overflow queue */
  1236. if (!list_empty(&wq->flusher_overflow)) {
  1237. /*
  1238. * Assign the same color to all overflowed
  1239. * flushers, advance work_color and append to
  1240. * flusher_queue. This is the start-to-wait
  1241. * phase for these overflowed flushers.
  1242. */
  1243. list_for_each_entry(tmp, &wq->flusher_overflow, list)
  1244. tmp->flush_color = wq->work_color;
  1245. wq->work_color = work_next_color(wq->work_color);
  1246. list_splice_tail_init(&wq->flusher_overflow,
  1247. &wq->flusher_queue);
  1248. flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
  1249. }
  1250. if (list_empty(&wq->flusher_queue)) {
  1251. BUG_ON(wq->flush_color != wq->work_color);
  1252. break;
  1253. }
  1254. /*
  1255. * Need to flush more colors. Make the next flusher
  1256. * the new first flusher and arm cwqs.
  1257. */
  1258. BUG_ON(wq->flush_color == wq->work_color);
  1259. BUG_ON(wq->flush_color != next->flush_color);
  1260. list_del_init(&next->list);
  1261. wq->first_flusher = next;
  1262. if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
  1263. break;
  1264. /*
  1265. * Meh... this color is already done, clear first
  1266. * flusher and repeat cascading.
  1267. */
  1268. wq->first_flusher = NULL;
  1269. }
  1270. out_unlock:
  1271. mutex_unlock(&wq->flush_mutex);
  1272. }
  1273. EXPORT_SYMBOL_GPL(flush_workqueue);
  1274. /**
  1275. * flush_work - block until a work_struct's callback has terminated
  1276. * @work: the work which is to be flushed
  1277. *
  1278. * Returns false if @work has already terminated.
  1279. *
  1280. * It is expected that, prior to calling flush_work(), the caller has
  1281. * arranged for the work to not be requeued, otherwise it doesn't make
  1282. * sense to use this function.
  1283. */
  1284. int flush_work(struct work_struct *work)
  1285. {
  1286. struct worker *worker = NULL;
  1287. struct global_cwq *gcwq;
  1288. struct cpu_workqueue_struct *cwq;
  1289. struct wq_barrier barr;
  1290. might_sleep();
  1291. gcwq = get_work_gcwq(work);
  1292. if (!gcwq)
  1293. return 0;
  1294. spin_lock_irq(&gcwq->lock);
  1295. if (!list_empty(&work->entry)) {
  1296. /*
  1297. * See the comment near try_to_grab_pending()->smp_rmb().
  1298. * If it was re-queued to a different gcwq under us, we
  1299. * are not going to wait.
  1300. */
  1301. smp_rmb();
  1302. cwq = get_work_cwq(work);
  1303. if (unlikely(!cwq || gcwq != cwq->gcwq))
  1304. goto already_gone;
  1305. } else {
  1306. worker = find_worker_executing_work(gcwq, work);
  1307. if (!worker)
  1308. goto already_gone;
  1309. cwq = worker->current_cwq;
  1310. }
  1311. insert_wq_barrier(cwq, &barr, work, worker);
  1312. spin_unlock_irq(&gcwq->lock);
  1313. lock_map_acquire(&cwq->wq->lockdep_map);
  1314. lock_map_release(&cwq->wq->lockdep_map);
  1315. wait_for_completion(&barr.done);
  1316. destroy_work_on_stack(&barr.work);
  1317. return 1;
  1318. already_gone:
  1319. spin_unlock_irq(&gcwq->lock);
  1320. return 0;
  1321. }
  1322. EXPORT_SYMBOL_GPL(flush_work);
  1323. /*
  1324. * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
  1325. * so this work can't be re-armed in any way.
  1326. */
  1327. static int try_to_grab_pending(struct work_struct *work)
  1328. {
  1329. struct global_cwq *gcwq;
  1330. int ret = -1;
  1331. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  1332. return 0;
  1333. /*
  1334. * The queueing is in progress, or it is already queued. Try to
  1335. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  1336. */
  1337. gcwq = get_work_gcwq(work);
  1338. if (!gcwq)
  1339. return ret;
  1340. spin_lock_irq(&gcwq->lock);
  1341. if (!list_empty(&work->entry)) {
  1342. /*
  1343. * This work is queued, but perhaps we locked the wrong gcwq.
  1344. * In that case we must see the new value after rmb(), see
  1345. * insert_work()->wmb().
  1346. */
  1347. smp_rmb();
  1348. if (gcwq == get_work_gcwq(work)) {
  1349. debug_work_deactivate(work);
  1350. list_del_init(&work->entry);
  1351. cwq_dec_nr_in_flight(get_work_cwq(work),
  1352. get_work_color(work));
  1353. ret = 1;
  1354. }
  1355. }
  1356. spin_unlock_irq(&gcwq->lock);
  1357. return ret;
  1358. }
  1359. static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
  1360. {
  1361. struct wq_barrier barr;
  1362. struct worker *worker;
  1363. spin_lock_irq(&gcwq->lock);
  1364. worker = find_worker_executing_work(gcwq, work);
  1365. if (unlikely(worker))
  1366. insert_wq_barrier(worker->current_cwq, &barr, work, worker);
  1367. spin_unlock_irq(&gcwq->lock);
  1368. if (unlikely(worker)) {
  1369. wait_for_completion(&barr.done);
  1370. destroy_work_on_stack(&barr.work);
  1371. }
  1372. }
  1373. static void wait_on_work(struct work_struct *work)
  1374. {
  1375. int cpu;
  1376. might_sleep();
  1377. lock_map_acquire(&work->lockdep_map);
  1378. lock_map_release(&work->lockdep_map);
  1379. for_each_possible_cpu(cpu)
  1380. wait_on_cpu_work(get_gcwq(cpu), work);
  1381. }
  1382. static int __cancel_work_timer(struct work_struct *work,
  1383. struct timer_list* timer)
  1384. {
  1385. int ret;
  1386. do {
  1387. ret = (timer && likely(del_timer(timer)));
  1388. if (!ret)
  1389. ret = try_to_grab_pending(work);
  1390. wait_on_work(work);
  1391. } while (unlikely(ret < 0));
  1392. clear_work_data(work);
  1393. return ret;
  1394. }
  1395. /**
  1396. * cancel_work_sync - block until a work_struct's callback has terminated
  1397. * @work: the work which is to be flushed
  1398. *
  1399. * Returns true if @work was pending.
  1400. *
  1401. * cancel_work_sync() will cancel the work if it is queued. If the work's
  1402. * callback appears to be running, cancel_work_sync() will block until it
  1403. * has completed.
  1404. *
  1405. * It is possible to use this function if the work re-queues itself. It can
  1406. * cancel the work even if it migrates to another workqueue, however in that
  1407. * case it only guarantees that work->func() has completed on the last queued
  1408. * workqueue.
  1409. *
  1410. * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
  1411. * pending, otherwise it goes into a busy-wait loop until the timer expires.
  1412. *
  1413. * The caller must ensure that workqueue_struct on which this work was last
  1414. * queued can't be destroyed before this function returns.
  1415. */
  1416. int cancel_work_sync(struct work_struct *work)
  1417. {
  1418. return __cancel_work_timer(work, NULL);
  1419. }
  1420. EXPORT_SYMBOL_GPL(cancel_work_sync);
  1421. /**
  1422. * cancel_delayed_work_sync - reliably kill off a delayed work.
  1423. * @dwork: the delayed work struct
  1424. *
  1425. * Returns true if @dwork was pending.
  1426. *
  1427. * It is possible to use this function if @dwork rearms itself via queue_work()
  1428. * or queue_delayed_work(). See also the comment for cancel_work_sync().
  1429. */
  1430. int cancel_delayed_work_sync(struct delayed_work *dwork)
  1431. {
  1432. return __cancel_work_timer(&dwork->work, &dwork->timer);
  1433. }
  1434. EXPORT_SYMBOL(cancel_delayed_work_sync);
  1435. static struct workqueue_struct *keventd_wq __read_mostly;
  1436. /**
  1437. * schedule_work - put work task in global workqueue
  1438. * @work: job to be done
  1439. *
  1440. * Returns zero if @work was already on the kernel-global workqueue and
  1441. * non-zero otherwise.
  1442. *
  1443. * This puts a job in the kernel-global workqueue if it was not already
  1444. * queued and leaves it in the same position on the kernel-global
  1445. * workqueue otherwise.
  1446. */
  1447. int schedule_work(struct work_struct *work)
  1448. {
  1449. return queue_work(keventd_wq, work);
  1450. }
  1451. EXPORT_SYMBOL(schedule_work);
  1452. /*
  1453. * schedule_work_on - put work task on a specific cpu
  1454. * @cpu: cpu to put the work task on
  1455. * @work: job to be done
  1456. *
  1457. * This puts a job on a specific cpu
  1458. */
  1459. int schedule_work_on(int cpu, struct work_struct *work)
  1460. {
  1461. return queue_work_on(cpu, keventd_wq, work);
  1462. }
  1463. EXPORT_SYMBOL(schedule_work_on);
  1464. /**
  1465. * schedule_delayed_work - put work task in global workqueue after delay
  1466. * @dwork: job to be done
  1467. * @delay: number of jiffies to wait or 0 for immediate execution
  1468. *
  1469. * After waiting for a given time this puts a job in the kernel-global
  1470. * workqueue.
  1471. */
  1472. int schedule_delayed_work(struct delayed_work *dwork,
  1473. unsigned long delay)
  1474. {
  1475. return queue_delayed_work(keventd_wq, dwork, delay);
  1476. }
  1477. EXPORT_SYMBOL(schedule_delayed_work);
  1478. /**
  1479. * flush_delayed_work - block until a dwork_struct's callback has terminated
  1480. * @dwork: the delayed work which is to be flushed
  1481. *
  1482. * Any timeout is cancelled, and any pending work is run immediately.
  1483. */
  1484. void flush_delayed_work(struct delayed_work *dwork)
  1485. {
  1486. if (del_timer_sync(&dwork->timer)) {
  1487. __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq,
  1488. &dwork->work);
  1489. put_cpu();
  1490. }
  1491. flush_work(&dwork->work);
  1492. }
  1493. EXPORT_SYMBOL(flush_delayed_work);
  1494. /**
  1495. * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
  1496. * @cpu: cpu to use
  1497. * @dwork: job to be done
  1498. * @delay: number of jiffies to wait
  1499. *
  1500. * After waiting for a given time this puts a job in the kernel-global
  1501. * workqueue on the specified CPU.
  1502. */
  1503. int schedule_delayed_work_on(int cpu,
  1504. struct delayed_work *dwork, unsigned long delay)
  1505. {
  1506. return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
  1507. }
  1508. EXPORT_SYMBOL(schedule_delayed_work_on);
  1509. /**
  1510. * schedule_on_each_cpu - call a function on each online CPU from keventd
  1511. * @func: the function to call
  1512. *
  1513. * Returns zero on success.
  1514. * Returns -ve errno on failure.
  1515. *
  1516. * schedule_on_each_cpu() is very slow.
  1517. */
  1518. int schedule_on_each_cpu(work_func_t func)
  1519. {
  1520. int cpu;
  1521. int orig = -1;
  1522. struct work_struct *works;
  1523. works = alloc_percpu(struct work_struct);
  1524. if (!works)
  1525. return -ENOMEM;
  1526. get_online_cpus();
  1527. /*
  1528. * When running in keventd don't schedule a work item on
  1529. * itself. Can just call directly because the work queue is
  1530. * already bound. This also is faster.
  1531. */
  1532. if (current_is_keventd())
  1533. orig = raw_smp_processor_id();
  1534. for_each_online_cpu(cpu) {
  1535. struct work_struct *work = per_cpu_ptr(works, cpu);
  1536. INIT_WORK(work, func);
  1537. if (cpu != orig)
  1538. schedule_work_on(cpu, work);
  1539. }
  1540. if (orig >= 0)
  1541. func(per_cpu_ptr(works, orig));
  1542. for_each_online_cpu(cpu)
  1543. flush_work(per_cpu_ptr(works, cpu));
  1544. put_online_cpus();
  1545. free_percpu(works);
  1546. return 0;
  1547. }
  1548. /**
  1549. * flush_scheduled_work - ensure that any scheduled work has run to completion.
  1550. *
  1551. * Forces execution of the kernel-global workqueue and blocks until its
  1552. * completion.
  1553. *
  1554. * Think twice before calling this function! It's very easy to get into
  1555. * trouble if you don't take great care. Either of the following situations
  1556. * will lead to deadlock:
  1557. *
  1558. * One of the work items currently on the workqueue needs to acquire
  1559. * a lock held by your code or its caller.
  1560. *
  1561. * Your code is running in the context of a work routine.
  1562. *
  1563. * They will be detected by lockdep when they occur, but the first might not
  1564. * occur very often. It depends on what work items are on the workqueue and
  1565. * what locks they need, which you have no control over.
  1566. *
  1567. * In most situations flushing the entire workqueue is overkill; you merely
  1568. * need to know that a particular work item isn't queued and isn't running.
  1569. * In such cases you should use cancel_delayed_work_sync() or
  1570. * cancel_work_sync() instead.
  1571. */
  1572. void flush_scheduled_work(void)
  1573. {
  1574. flush_workqueue(keventd_wq);
  1575. }
  1576. EXPORT_SYMBOL(flush_scheduled_work);
  1577. /**
  1578. * execute_in_process_context - reliably execute the routine with user context
  1579. * @fn: the function to execute
  1580. * @ew: guaranteed storage for the execute work structure (must
  1581. * be available when the work executes)
  1582. *
  1583. * Executes the function immediately if process context is available,
  1584. * otherwise schedules the function for delayed execution.
  1585. *
  1586. * Returns: 0 - function was executed
  1587. * 1 - function was scheduled for execution
  1588. */
  1589. int execute_in_process_context(work_func_t fn, struct execute_work *ew)
  1590. {
  1591. if (!in_interrupt()) {
  1592. fn(&ew->work);
  1593. return 0;
  1594. }
  1595. INIT_WORK(&ew->work, fn);
  1596. schedule_work(&ew->work);
  1597. return 1;
  1598. }
  1599. EXPORT_SYMBOL_GPL(execute_in_process_context);
  1600. int keventd_up(void)
  1601. {
  1602. return keventd_wq != NULL;
  1603. }
  1604. int current_is_keventd(void)
  1605. {
  1606. struct cpu_workqueue_struct *cwq;
  1607. int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
  1608. int ret = 0;
  1609. BUG_ON(!keventd_wq);
  1610. cwq = get_cwq(cpu, keventd_wq);
  1611. if (current == cwq->worker->task)
  1612. ret = 1;
  1613. return ret;
  1614. }
  1615. static struct cpu_workqueue_struct *alloc_cwqs(void)
  1616. {
  1617. /*
  1618. * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
  1619. * Make sure that the alignment isn't lower than that of
  1620. * unsigned long long.
  1621. */
  1622. const size_t size = sizeof(struct cpu_workqueue_struct);
  1623. const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
  1624. __alignof__(unsigned long long));
  1625. struct cpu_workqueue_struct *cwqs;
  1626. #ifndef CONFIG_SMP
  1627. void *ptr;
  1628. /*
  1629. * On UP, percpu allocator doesn't honor alignment parameter
  1630. * and simply uses arch-dependent default. Allocate enough
  1631. * room to align cwq and put an extra pointer at the end
  1632. * pointing back to the originally allocated pointer which
  1633. * will be used for free.
  1634. *
  1635. * FIXME: This really belongs to UP percpu code. Update UP
  1636. * percpu code to honor alignment and remove this ugliness.
  1637. */
  1638. ptr = __alloc_percpu(size + align + sizeof(void *), 1);
  1639. cwqs = PTR_ALIGN(ptr, align);
  1640. *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
  1641. #else
  1642. /* On SMP, percpu allocator can do it itself */
  1643. cwqs = __alloc_percpu(size, align);
  1644. #endif
  1645. /* just in case, make sure it's actually aligned */
  1646. BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
  1647. return cwqs;
  1648. }
  1649. static void free_cwqs(struct cpu_workqueue_struct *cwqs)
  1650. {
  1651. #ifndef CONFIG_SMP
  1652. /* on UP, the pointer to free is stored right after the cwq */
  1653. if (cwqs)
  1654. free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
  1655. #else
  1656. free_percpu(cwqs);
  1657. #endif
  1658. }
  1659. struct workqueue_struct *__create_workqueue_key(const char *name,
  1660. unsigned int flags,
  1661. int max_active,
  1662. struct lock_class_key *key,
  1663. const char *lock_name)
  1664. {
  1665. struct workqueue_struct *wq;
  1666. bool failed = false;
  1667. unsigned int cpu;
  1668. max_active = clamp_val(max_active, 1, INT_MAX);
  1669. wq = kzalloc(sizeof(*wq), GFP_KERNEL);
  1670. if (!wq)
  1671. goto err;
  1672. wq->cpu_wq = alloc_cwqs();
  1673. if (!wq->cpu_wq)
  1674. goto err;
  1675. wq->flags = flags;
  1676. wq->saved_max_active = max_active;
  1677. mutex_init(&wq->flush_mutex);
  1678. atomic_set(&wq->nr_cwqs_to_flush, 0);
  1679. INIT_LIST_HEAD(&wq->flusher_queue);
  1680. INIT_LIST_HEAD(&wq->flusher_overflow);
  1681. wq->single_cpu = NR_CPUS;
  1682. wq->name = name;
  1683. lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
  1684. INIT_LIST_HEAD(&wq->list);
  1685. cpu_maps_update_begin();
  1686. /*
  1687. * We must initialize cwqs for each possible cpu even if we
  1688. * are going to call destroy_workqueue() finally. Otherwise
  1689. * cpu_up() can hit the uninitialized cwq once we drop the
  1690. * lock.
  1691. */
  1692. for_each_possible_cpu(cpu) {
  1693. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1694. struct global_cwq *gcwq = get_gcwq(cpu);
  1695. BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
  1696. cwq->gcwq = gcwq;
  1697. cwq->wq = wq;
  1698. cwq->flush_color = -1;
  1699. cwq->max_active = max_active;
  1700. INIT_LIST_HEAD(&cwq->worklist);
  1701. INIT_LIST_HEAD(&cwq->delayed_works);
  1702. if (failed)
  1703. continue;
  1704. cwq->worker = create_worker(cwq, cpu_online(cpu));
  1705. if (cwq->worker)
  1706. start_worker(cwq->worker);
  1707. else
  1708. failed = true;
  1709. }
  1710. /*
  1711. * workqueue_lock protects global freeze state and workqueues
  1712. * list. Grab it, set max_active accordingly and add the new
  1713. * workqueue to workqueues list.
  1714. */
  1715. spin_lock(&workqueue_lock);
  1716. if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
  1717. for_each_possible_cpu(cpu)
  1718. get_cwq(cpu, wq)->max_active = 0;
  1719. list_add(&wq->list, &workqueues);
  1720. spin_unlock(&workqueue_lock);
  1721. cpu_maps_update_done();
  1722. if (failed) {
  1723. destroy_workqueue(wq);
  1724. wq = NULL;
  1725. }
  1726. return wq;
  1727. err:
  1728. if (wq) {
  1729. free_cwqs(wq->cpu_wq);
  1730. kfree(wq);
  1731. }
  1732. return NULL;
  1733. }
  1734. EXPORT_SYMBOL_GPL(__create_workqueue_key);
  1735. /**
  1736. * destroy_workqueue - safely terminate a workqueue
  1737. * @wq: target workqueue
  1738. *
  1739. * Safely destroy a workqueue. All work currently pending will be done first.
  1740. */
  1741. void destroy_workqueue(struct workqueue_struct *wq)
  1742. {
  1743. unsigned int cpu;
  1744. flush_workqueue(wq);
  1745. /*
  1746. * wq list is used to freeze wq, remove from list after
  1747. * flushing is complete in case freeze races us.
  1748. */
  1749. cpu_maps_update_begin();
  1750. spin_lock(&workqueue_lock);
  1751. list_del(&wq->list);
  1752. spin_unlock(&workqueue_lock);
  1753. cpu_maps_update_done();
  1754. for_each_possible_cpu(cpu) {
  1755. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  1756. int i;
  1757. if (cwq->worker) {
  1758. spin_lock_irq(&cwq->gcwq->lock);
  1759. destroy_worker(cwq->worker);
  1760. cwq->worker = NULL;
  1761. spin_unlock_irq(&cwq->gcwq->lock);
  1762. }
  1763. for (i = 0; i < WORK_NR_COLORS; i++)
  1764. BUG_ON(cwq->nr_in_flight[i]);
  1765. BUG_ON(cwq->nr_active);
  1766. BUG_ON(!list_empty(&cwq->delayed_works));
  1767. }
  1768. free_cwqs(wq->cpu_wq);
  1769. kfree(wq);
  1770. }
  1771. EXPORT_SYMBOL_GPL(destroy_workqueue);
  1772. /*
  1773. * CPU hotplug.
  1774. *
  1775. * CPU hotplug is implemented by allowing cwqs to be detached from
  1776. * CPU, running with unbound workers and allowing them to be
  1777. * reattached later if the cpu comes back online. A separate thread
  1778. * is created to govern cwqs in such state and is called the trustee.
  1779. *
  1780. * Trustee states and their descriptions.
  1781. *
  1782. * START Command state used on startup. On CPU_DOWN_PREPARE, a
  1783. * new trustee is started with this state.
  1784. *
  1785. * IN_CHARGE Once started, trustee will enter this state after
  1786. * making all existing workers rogue. DOWN_PREPARE waits
  1787. * for trustee to enter this state. After reaching
  1788. * IN_CHARGE, trustee tries to execute the pending
  1789. * worklist until it's empty and the state is set to
  1790. * BUTCHER, or the state is set to RELEASE.
  1791. *
  1792. * BUTCHER Command state which is set by the cpu callback after
  1793. * the cpu has went down. Once this state is set trustee
  1794. * knows that there will be no new works on the worklist
  1795. * and once the worklist is empty it can proceed to
  1796. * killing idle workers.
  1797. *
  1798. * RELEASE Command state which is set by the cpu callback if the
  1799. * cpu down has been canceled or it has come online
  1800. * again. After recognizing this state, trustee stops
  1801. * trying to drain or butcher and transits to DONE.
  1802. *
  1803. * DONE Trustee will enter this state after BUTCHER or RELEASE
  1804. * is complete.
  1805. *
  1806. * trustee CPU draining
  1807. * took over down complete
  1808. * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
  1809. * | | ^
  1810. * | CPU is back online v return workers |
  1811. * ----------------> RELEASE --------------
  1812. */
  1813. /**
  1814. * trustee_wait_event_timeout - timed event wait for trustee
  1815. * @cond: condition to wait for
  1816. * @timeout: timeout in jiffies
  1817. *
  1818. * wait_event_timeout() for trustee to use. Handles locking and
  1819. * checks for RELEASE request.
  1820. *
  1821. * CONTEXT:
  1822. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1823. * multiple times. To be used by trustee.
  1824. *
  1825. * RETURNS:
  1826. * Positive indicating left time if @cond is satisfied, 0 if timed
  1827. * out, -1 if canceled.
  1828. */
  1829. #define trustee_wait_event_timeout(cond, timeout) ({ \
  1830. long __ret = (timeout); \
  1831. while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
  1832. __ret) { \
  1833. spin_unlock_irq(&gcwq->lock); \
  1834. __wait_event_timeout(gcwq->trustee_wait, (cond) || \
  1835. (gcwq->trustee_state == TRUSTEE_RELEASE), \
  1836. __ret); \
  1837. spin_lock_irq(&gcwq->lock); \
  1838. } \
  1839. gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
  1840. })
  1841. /**
  1842. * trustee_wait_event - event wait for trustee
  1843. * @cond: condition to wait for
  1844. *
  1845. * wait_event() for trustee to use. Automatically handles locking and
  1846. * checks for CANCEL request.
  1847. *
  1848. * CONTEXT:
  1849. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1850. * multiple times. To be used by trustee.
  1851. *
  1852. * RETURNS:
  1853. * 0 if @cond is satisfied, -1 if canceled.
  1854. */
  1855. #define trustee_wait_event(cond) ({ \
  1856. long __ret1; \
  1857. __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
  1858. __ret1 < 0 ? -1 : 0; \
  1859. })
  1860. static int __cpuinit trustee_thread(void *__gcwq)
  1861. {
  1862. struct global_cwq *gcwq = __gcwq;
  1863. struct worker *worker;
  1864. struct hlist_node *pos;
  1865. int i;
  1866. BUG_ON(gcwq->cpu != smp_processor_id());
  1867. spin_lock_irq(&gcwq->lock);
  1868. /*
  1869. * Make all workers rogue. Trustee must be bound to the
  1870. * target cpu and can't be cancelled.
  1871. */
  1872. BUG_ON(gcwq->cpu != smp_processor_id());
  1873. list_for_each_entry(worker, &gcwq->idle_list, entry)
  1874. worker->flags |= WORKER_ROGUE;
  1875. for_each_busy_worker(worker, i, pos, gcwq)
  1876. worker->flags |= WORKER_ROGUE;
  1877. /*
  1878. * We're now in charge. Notify and proceed to drain. We need
  1879. * to keep the gcwq running during the whole CPU down
  1880. * procedure as other cpu hotunplug callbacks may need to
  1881. * flush currently running tasks.
  1882. */
  1883. gcwq->trustee_state = TRUSTEE_IN_CHARGE;
  1884. wake_up_all(&gcwq->trustee_wait);
  1885. /*
  1886. * The original cpu is in the process of dying and may go away
  1887. * anytime now. When that happens, we and all workers would
  1888. * be migrated to other cpus. Try draining any left work.
  1889. * Note that if the gcwq is frozen, there may be frozen works
  1890. * in freezeable cwqs. Don't declare completion while frozen.
  1891. */
  1892. while (gcwq->nr_workers != gcwq->nr_idle ||
  1893. gcwq->flags & GCWQ_FREEZING ||
  1894. gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
  1895. /* give a breather */
  1896. if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
  1897. break;
  1898. }
  1899. /* notify completion */
  1900. gcwq->trustee = NULL;
  1901. gcwq->trustee_state = TRUSTEE_DONE;
  1902. wake_up_all(&gcwq->trustee_wait);
  1903. spin_unlock_irq(&gcwq->lock);
  1904. return 0;
  1905. }
  1906. /**
  1907. * wait_trustee_state - wait for trustee to enter the specified state
  1908. * @gcwq: gcwq the trustee of interest belongs to
  1909. * @state: target state to wait for
  1910. *
  1911. * Wait for the trustee to reach @state. DONE is already matched.
  1912. *
  1913. * CONTEXT:
  1914. * spin_lock_irq(gcwq->lock) which may be released and regrabbed
  1915. * multiple times. To be used by cpu_callback.
  1916. */
  1917. static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
  1918. {
  1919. if (!(gcwq->trustee_state == state ||
  1920. gcwq->trustee_state == TRUSTEE_DONE)) {
  1921. spin_unlock_irq(&gcwq->lock);
  1922. __wait_event(gcwq->trustee_wait,
  1923. gcwq->trustee_state == state ||
  1924. gcwq->trustee_state == TRUSTEE_DONE);
  1925. spin_lock_irq(&gcwq->lock);
  1926. }
  1927. }
  1928. static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
  1929. unsigned long action,
  1930. void *hcpu)
  1931. {
  1932. unsigned int cpu = (unsigned long)hcpu;
  1933. struct global_cwq *gcwq = get_gcwq(cpu);
  1934. struct task_struct *new_trustee = NULL;
  1935. struct worker *worker;
  1936. struct hlist_node *pos;
  1937. unsigned long flags;
  1938. int i;
  1939. action &= ~CPU_TASKS_FROZEN;
  1940. switch (action) {
  1941. case CPU_DOWN_PREPARE:
  1942. new_trustee = kthread_create(trustee_thread, gcwq,
  1943. "workqueue_trustee/%d\n", cpu);
  1944. if (IS_ERR(new_trustee))
  1945. return notifier_from_errno(PTR_ERR(new_trustee));
  1946. kthread_bind(new_trustee, cpu);
  1947. }
  1948. /* some are called w/ irq disabled, don't disturb irq status */
  1949. spin_lock_irqsave(&gcwq->lock, flags);
  1950. switch (action) {
  1951. case CPU_DOWN_PREPARE:
  1952. /* initialize trustee and tell it to acquire the gcwq */
  1953. BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
  1954. gcwq->trustee = new_trustee;
  1955. gcwq->trustee_state = TRUSTEE_START;
  1956. wake_up_process(gcwq->trustee);
  1957. wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
  1958. break;
  1959. case CPU_POST_DEAD:
  1960. gcwq->trustee_state = TRUSTEE_BUTCHER;
  1961. break;
  1962. case CPU_DOWN_FAILED:
  1963. case CPU_ONLINE:
  1964. if (gcwq->trustee_state != TRUSTEE_DONE) {
  1965. gcwq->trustee_state = TRUSTEE_RELEASE;
  1966. wake_up_process(gcwq->trustee);
  1967. wait_trustee_state(gcwq, TRUSTEE_DONE);
  1968. }
  1969. /* clear ROGUE from all workers */
  1970. list_for_each_entry(worker, &gcwq->idle_list, entry)
  1971. worker->flags &= ~WORKER_ROGUE;
  1972. for_each_busy_worker(worker, i, pos, gcwq)
  1973. worker->flags &= ~WORKER_ROGUE;
  1974. break;
  1975. }
  1976. spin_unlock_irqrestore(&gcwq->lock, flags);
  1977. return notifier_from_errno(0);
  1978. }
  1979. #ifdef CONFIG_SMP
  1980. struct work_for_cpu {
  1981. struct completion completion;
  1982. long (*fn)(void *);
  1983. void *arg;
  1984. long ret;
  1985. };
  1986. static int do_work_for_cpu(void *_wfc)
  1987. {
  1988. struct work_for_cpu *wfc = _wfc;
  1989. wfc->ret = wfc->fn(wfc->arg);
  1990. complete(&wfc->completion);
  1991. return 0;
  1992. }
  1993. /**
  1994. * work_on_cpu - run a function in user context on a particular cpu
  1995. * @cpu: the cpu to run on
  1996. * @fn: the function to run
  1997. * @arg: the function arg
  1998. *
  1999. * This will return the value @fn returns.
  2000. * It is up to the caller to ensure that the cpu doesn't go offline.
  2001. * The caller must not hold any locks which would prevent @fn from completing.
  2002. */
  2003. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  2004. {
  2005. struct task_struct *sub_thread;
  2006. struct work_for_cpu wfc = {
  2007. .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
  2008. .fn = fn,
  2009. .arg = arg,
  2010. };
  2011. sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
  2012. if (IS_ERR(sub_thread))
  2013. return PTR_ERR(sub_thread);
  2014. kthread_bind(sub_thread, cpu);
  2015. wake_up_process(sub_thread);
  2016. wait_for_completion(&wfc.completion);
  2017. return wfc.ret;
  2018. }
  2019. EXPORT_SYMBOL_GPL(work_on_cpu);
  2020. #endif /* CONFIG_SMP */
  2021. #ifdef CONFIG_FREEZER
  2022. /**
  2023. * freeze_workqueues_begin - begin freezing workqueues
  2024. *
  2025. * Start freezing workqueues. After this function returns, all
  2026. * freezeable workqueues will queue new works to their frozen_works
  2027. * list instead of the cwq ones.
  2028. *
  2029. * CONTEXT:
  2030. * Grabs and releases workqueue_lock and gcwq->lock's.
  2031. */
  2032. void freeze_workqueues_begin(void)
  2033. {
  2034. struct workqueue_struct *wq;
  2035. unsigned int cpu;
  2036. spin_lock(&workqueue_lock);
  2037. BUG_ON(workqueue_freezing);
  2038. workqueue_freezing = true;
  2039. for_each_possible_cpu(cpu) {
  2040. struct global_cwq *gcwq = get_gcwq(cpu);
  2041. spin_lock_irq(&gcwq->lock);
  2042. BUG_ON(gcwq->flags & GCWQ_FREEZING);
  2043. gcwq->flags |= GCWQ_FREEZING;
  2044. list_for_each_entry(wq, &workqueues, list) {
  2045. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  2046. if (wq->flags & WQ_FREEZEABLE)
  2047. cwq->max_active = 0;
  2048. }
  2049. spin_unlock_irq(&gcwq->lock);
  2050. }
  2051. spin_unlock(&workqueue_lock);
  2052. }
  2053. /**
  2054. * freeze_workqueues_busy - are freezeable workqueues still busy?
  2055. *
  2056. * Check whether freezing is complete. This function must be called
  2057. * between freeze_workqueues_begin() and thaw_workqueues().
  2058. *
  2059. * CONTEXT:
  2060. * Grabs and releases workqueue_lock.
  2061. *
  2062. * RETURNS:
  2063. * %true if some freezeable workqueues are still busy. %false if
  2064. * freezing is complete.
  2065. */
  2066. bool freeze_workqueues_busy(void)
  2067. {
  2068. struct workqueue_struct *wq;
  2069. unsigned int cpu;
  2070. bool busy = false;
  2071. spin_lock(&workqueue_lock);
  2072. BUG_ON(!workqueue_freezing);
  2073. for_each_possible_cpu(cpu) {
  2074. /*
  2075. * nr_active is monotonically decreasing. It's safe
  2076. * to peek without lock.
  2077. */
  2078. list_for_each_entry(wq, &workqueues, list) {
  2079. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  2080. if (!(wq->flags & WQ_FREEZEABLE))
  2081. continue;
  2082. BUG_ON(cwq->nr_active < 0);
  2083. if (cwq->nr_active) {
  2084. busy = true;
  2085. goto out_unlock;
  2086. }
  2087. }
  2088. }
  2089. out_unlock:
  2090. spin_unlock(&workqueue_lock);
  2091. return busy;
  2092. }
  2093. /**
  2094. * thaw_workqueues - thaw workqueues
  2095. *
  2096. * Thaw workqueues. Normal queueing is restored and all collected
  2097. * frozen works are transferred to their respective cwq worklists.
  2098. *
  2099. * CONTEXT:
  2100. * Grabs and releases workqueue_lock and gcwq->lock's.
  2101. */
  2102. void thaw_workqueues(void)
  2103. {
  2104. struct workqueue_struct *wq;
  2105. unsigned int cpu;
  2106. spin_lock(&workqueue_lock);
  2107. if (!workqueue_freezing)
  2108. goto out_unlock;
  2109. for_each_possible_cpu(cpu) {
  2110. struct global_cwq *gcwq = get_gcwq(cpu);
  2111. spin_lock_irq(&gcwq->lock);
  2112. BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
  2113. gcwq->flags &= ~GCWQ_FREEZING;
  2114. list_for_each_entry(wq, &workqueues, list) {
  2115. struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
  2116. if (!(wq->flags & WQ_FREEZEABLE))
  2117. continue;
  2118. /* restore max_active and repopulate worklist */
  2119. cwq->max_active = wq->saved_max_active;
  2120. while (!list_empty(&cwq->delayed_works) &&
  2121. cwq->nr_active < cwq->max_active)
  2122. cwq_activate_first_delayed(cwq);
  2123. /* perform delayed unbind from single cpu if empty */
  2124. if (wq->single_cpu == gcwq->cpu &&
  2125. !cwq->nr_active && list_empty(&cwq->delayed_works))
  2126. cwq_unbind_single_cpu(cwq);
  2127. wake_up_process(cwq->worker->task);
  2128. }
  2129. spin_unlock_irq(&gcwq->lock);
  2130. }
  2131. workqueue_freezing = false;
  2132. out_unlock:
  2133. spin_unlock(&workqueue_lock);
  2134. }
  2135. #endif /* CONFIG_FREEZER */
  2136. void __init init_workqueues(void)
  2137. {
  2138. unsigned int cpu;
  2139. int i;
  2140. /*
  2141. * The pointer part of work->data is either pointing to the
  2142. * cwq or contains the cpu number the work ran last on. Make
  2143. * sure cpu number won't overflow into kernel pointer area so
  2144. * that they can be distinguished.
  2145. */
  2146. BUILD_BUG_ON(NR_CPUS << WORK_STRUCT_FLAG_BITS >= PAGE_OFFSET);
  2147. hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
  2148. /* initialize gcwqs */
  2149. for_each_possible_cpu(cpu) {
  2150. struct global_cwq *gcwq = get_gcwq(cpu);
  2151. spin_lock_init(&gcwq->lock);
  2152. gcwq->cpu = cpu;
  2153. INIT_LIST_HEAD(&gcwq->idle_list);
  2154. for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
  2155. INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
  2156. ida_init(&gcwq->worker_ida);
  2157. gcwq->trustee_state = TRUSTEE_DONE;
  2158. init_waitqueue_head(&gcwq->trustee_wait);
  2159. }
  2160. keventd_wq = create_workqueue("events");
  2161. BUG_ON(!keventd_wq);
  2162. }