workqueue.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. =========
  2. Workqueue
  3. =========
  4. :Date: September, 2010
  5. :Author: Tejun Heo <tj@kernel.org>
  6. :Author: Florian Mickler <florian@mickler.org>
  7. Introduction
  8. ============
  9. There are many cases where an asynchronous process execution context
  10. is needed and the workqueue (wq) API is the most commonly used
  11. mechanism for such cases.
  12. When such an asynchronous execution context is needed, a work item
  13. describing which function to execute is put on a queue. An
  14. independent thread serves as the asynchronous execution context. The
  15. queue is called workqueue and the thread is called worker.
  16. While there are work items on the workqueue the worker executes the
  17. functions associated with the work items one after the other. When
  18. there is no work item left on the workqueue the worker becomes idle.
  19. When a new work item gets queued, the worker begins executing again.
  20. Why Concurrency Managed Workqueue?
  21. ==================================
  22. In the original wq implementation, a multi threaded (MT) wq had one
  23. worker thread per CPU and a single threaded (ST) wq had one worker
  24. thread system-wide. A single MT wq needed to keep around the same
  25. number of workers as the number of CPUs. The kernel grew a lot of MT
  26. wq users over the years and with the number of CPU cores continuously
  27. rising, some systems saturated the default 32k PID space just booting
  28. up.
  29. Although MT wq wasted a lot of resource, the level of concurrency
  30. provided was unsatisfactory. The limitation was common to both ST and
  31. MT wq albeit less severe on MT. Each wq maintained its own separate
  32. worker pool. An MT wq could provide only one execution context per CPU
  33. while an ST wq one for the whole system. Work items had to compete for
  34. those very limited execution contexts leading to various problems
  35. including proneness to deadlocks around the single execution context.
  36. The tension between the provided level of concurrency and resource
  37. usage also forced its users to make unnecessary tradeoffs like libata
  38. choosing to use ST wq for polling PIOs and accepting an unnecessary
  39. limitation that no two polling PIOs can progress at the same time. As
  40. MT wq don't provide much better concurrency, users which require
  41. higher level of concurrency, like async or fscache, had to implement
  42. their own thread pool.
  43. Concurrency Managed Workqueue (cmwq) is a reimplementation of wq with
  44. focus on the following goals.
  45. * Maintain compatibility with the original workqueue API.
  46. * Use per-CPU unified worker pools shared by all wq to provide
  47. flexible level of concurrency on demand without wasting a lot of
  48. resource.
  49. * Automatically regulate worker pool and level of concurrency so that
  50. the API users don't need to worry about such details.
  51. The Design
  52. ==========
  53. In order to ease the asynchronous execution of functions a new
  54. abstraction, the work item, is introduced.
  55. A work item is a simple struct that holds a pointer to the function
  56. that is to be executed asynchronously. Whenever a driver or subsystem
  57. wants a function to be executed asynchronously it has to set up a work
  58. item pointing to that function and queue that work item on a
  59. workqueue.
  60. A work item can be executed in either a thread or the BH (softirq) context.
  61. For threaded workqueues, special purpose threads, called [k]workers, execute
  62. the functions off of the queue, one after the other. If no work is queued,
  63. the worker threads become idle. These worker threads are managed in
  64. worker-pools.
  65. The cmwq design differentiates between the user-facing workqueues that
  66. subsystems and drivers queue work items on and the backend mechanism
  67. which manages worker-pools and processes the queued work items.
  68. There are two worker-pools, one for normal work items and the other
  69. for high priority ones, for each possible CPU and some extra
  70. worker-pools to serve work items queued on unbound workqueues - the
  71. number of these backing pools is dynamic.
  72. BH workqueues use the same framework. However, as there can only be one
  73. concurrent execution context, there's no need to worry about concurrency.
  74. Each per-CPU BH worker pool contains only one pseudo worker which represents
  75. the BH execution context. A BH workqueue can be considered a convenience
  76. interface to softirq.
  77. Subsystems and drivers can create and queue work items through special
  78. workqueue API functions as they see fit. They can influence some
  79. aspects of the way the work items are executed by setting flags on the
  80. workqueue they are putting the work item on. These flags include
  81. things like CPU locality, concurrency limits, priority and more. To
  82. get a detailed overview refer to the API description of
  83. ``alloc_workqueue()`` below.
  84. When a work item is queued to a workqueue, the target worker-pool is
  85. determined according to the queue parameters and workqueue attributes
  86. and appended on the shared worklist of the worker-pool. For example,
  87. unless specifically overridden, a work item of a bound workqueue will
  88. be queued on the worklist of either normal or highpri worker-pool that
  89. is associated to the CPU the issuer is running on.
  90. For any thread pool implementation, managing the concurrency level
  91. (how many execution contexts are active) is an important issue. cmwq
  92. tries to keep the concurrency at a minimal but sufficient level.
  93. Minimal to save resources and sufficient in that the system is used at
  94. its full capacity.
  95. Each worker-pool bound to an actual CPU implements concurrency
  96. management by hooking into the scheduler. The worker-pool is notified
  97. whenever an active worker wakes up or sleeps and keeps track of the
  98. number of the currently runnable workers. Generally, work items are
  99. not expected to hog a CPU and consume many cycles. That means
  100. maintaining just enough concurrency to prevent work processing from
  101. stalling should be optimal. As long as there are one or more runnable
  102. workers on the CPU, the worker-pool doesn't start execution of a new
  103. work, but, when the last running worker goes to sleep, it immediately
  104. schedules a new worker so that the CPU doesn't sit idle while there
  105. are pending work items. This allows using a minimal number of workers
  106. without losing execution bandwidth.
  107. Keeping idle workers around doesn't cost other than the memory space
  108. for kthreads, so cmwq holds onto idle ones for a while before killing
  109. them.
  110. For unbound workqueues, the number of backing pools is dynamic.
  111. Unbound workqueue can be assigned custom attributes using
  112. ``apply_workqueue_attrs()`` and workqueue will automatically create
  113. backing worker pools matching the attributes. The responsibility of
  114. regulating concurrency level is on the users. There is also a flag to
  115. mark a bound wq to ignore the concurrency management. Please refer to
  116. the API section for details.
  117. Forward progress guarantee relies on that workers can be created when
  118. more execution contexts are necessary, which in turn is guaranteed
  119. through the use of rescue workers. All work items which might be used
  120. on code paths that handle memory reclaim are required to be queued on
  121. wq's that have a rescue-worker reserved for execution under memory
  122. pressure. Else it is possible that the worker-pool deadlocks waiting
  123. for execution contexts to free up.
  124. Application Programming Interface (API)
  125. =======================================
  126. ``alloc_workqueue()`` allocates a wq. The original
  127. ``create_*workqueue()`` functions are deprecated and scheduled for
  128. removal. ``alloc_workqueue()`` takes three arguments - ``@name``,
  129. ``@flags`` and ``@max_active``. ``@name`` is the name of the wq and
  130. also used as the name of the rescuer thread if there is one.
  131. A wq no longer manages execution resources but serves as a domain for
  132. forward progress guarantee, flush and work item attributes. ``@flags``
  133. and ``@max_active`` control how work items are assigned execution
  134. resources, scheduled and executed.
  135. ``flags``
  136. ---------
  137. ``WQ_BH``
  138. BH workqueues can be considered a convenience interface to softirq. BH
  139. workqueues are always per-CPU and all BH work items are executed in the
  140. queueing CPU's softirq context in the queueing order.
  141. All BH workqueues must have 0 ``max_active`` and ``WQ_HIGHPRI`` is the
  142. only allowed additional flag.
  143. BH work items cannot sleep. All other features such as delayed queueing,
  144. flushing and canceling are supported.
  145. ``WQ_UNBOUND``
  146. Work items queued to an unbound wq are served by the special
  147. worker-pools which host workers which are not bound to any
  148. specific CPU. This makes the wq behave as a simple execution
  149. context provider without concurrency management. The unbound
  150. worker-pools try to start execution of work items as soon as
  151. possible. Unbound wq sacrifices locality but is useful for
  152. the following cases.
  153. * Wide fluctuation in the concurrency level requirement is
  154. expected and using bound wq may end up creating large number
  155. of mostly unused workers across different CPUs as the issuer
  156. hops through different CPUs.
  157. * Long running CPU intensive workloads which can be better
  158. managed by the system scheduler.
  159. ``WQ_FREEZABLE``
  160. A freezable wq participates in the freeze phase of the system
  161. suspend operations. Work items on the wq are drained and no
  162. new work item starts execution until thawed.
  163. ``WQ_MEM_RECLAIM``
  164. All wq which might be used in the memory reclaim paths **MUST**
  165. have this flag set. The wq is guaranteed to have at least one
  166. execution context regardless of memory pressure.
  167. ``WQ_HIGHPRI``
  168. Work items of a highpri wq are queued to the highpri
  169. worker-pool of the target cpu. Highpri worker-pools are
  170. served by worker threads with elevated nice level.
  171. Note that normal and highpri worker-pools don't interact with
  172. each other. Each maintains its separate pool of workers and
  173. implements concurrency management among its workers.
  174. ``WQ_CPU_INTENSIVE``
  175. Work items of a CPU intensive wq do not contribute to the
  176. concurrency level. In other words, runnable CPU intensive
  177. work items will not prevent other work items in the same
  178. worker-pool from starting execution. This is useful for bound
  179. work items which are expected to hog CPU cycles so that their
  180. execution is regulated by the system scheduler.
  181. Although CPU intensive work items don't contribute to the
  182. concurrency level, start of their executions is still
  183. regulated by the concurrency management and runnable
  184. non-CPU-intensive work items can delay execution of CPU
  185. intensive work items.
  186. This flag is meaningless for unbound wq.
  187. ``max_active``
  188. --------------
  189. ``@max_active`` determines the maximum number of execution contexts per
  190. CPU which can be assigned to the work items of a wq. For example, with
  191. ``@max_active`` of 16, at most 16 work items of the wq can be executing
  192. at the same time per CPU. This is always a per-CPU attribute, even for
  193. unbound workqueues.
  194. The maximum limit for ``@max_active`` is 512 and the default value used
  195. when 0 is specified is 256. These values are chosen sufficiently high
  196. such that they are not the limiting factor while providing protection in
  197. runaway cases.
  198. The number of active work items of a wq is usually regulated by the
  199. users of the wq, more specifically, by how many work items the users
  200. may queue at the same time. Unless there is a specific need for
  201. throttling the number of active work items, specifying '0' is
  202. recommended.
  203. Some users depend on strict execution ordering where only one work item
  204. is in flight at any given time and the work items are processed in
  205. queueing order. While the combination of ``@max_active`` of 1 and
  206. ``WQ_UNBOUND`` used to achieve this behavior, this is no longer the
  207. case. Use alloc_ordered_workqueue() instead.
  208. Example Execution Scenarios
  209. ===========================
  210. The following example execution scenarios try to illustrate how cmwq
  211. behave under different configurations.
  212. Work items w0, w1, w2 are queued to a bound wq q0 on the same CPU.
  213. w0 burns CPU for 5ms then sleeps for 10ms then burns CPU for 5ms
  214. again before finishing. w1 and w2 burn CPU for 5ms then sleep for
  215. 10ms.
  216. Ignoring all other tasks, works and processing overhead, and assuming
  217. simple FIFO scheduling, the following is one highly simplified version
  218. of possible sequences of events with the original wq. ::
  219. TIME IN MSECS EVENT
  220. 0 w0 starts and burns CPU
  221. 5 w0 sleeps
  222. 15 w0 wakes up and burns CPU
  223. 20 w0 finishes
  224. 20 w1 starts and burns CPU
  225. 25 w1 sleeps
  226. 35 w1 wakes up and finishes
  227. 35 w2 starts and burns CPU
  228. 40 w2 sleeps
  229. 50 w2 wakes up and finishes
  230. And with cmwq with ``@max_active`` >= 3, ::
  231. TIME IN MSECS EVENT
  232. 0 w0 starts and burns CPU
  233. 5 w0 sleeps
  234. 5 w1 starts and burns CPU
  235. 10 w1 sleeps
  236. 10 w2 starts and burns CPU
  237. 15 w2 sleeps
  238. 15 w0 wakes up and burns CPU
  239. 20 w0 finishes
  240. 20 w1 wakes up and finishes
  241. 25 w2 wakes up and finishes
  242. If ``@max_active`` == 2, ::
  243. TIME IN MSECS EVENT
  244. 0 w0 starts and burns CPU
  245. 5 w0 sleeps
  246. 5 w1 starts and burns CPU
  247. 10 w1 sleeps
  248. 15 w0 wakes up and burns CPU
  249. 20 w0 finishes
  250. 20 w1 wakes up and finishes
  251. 20 w2 starts and burns CPU
  252. 25 w2 sleeps
  253. 35 w2 wakes up and finishes
  254. Now, let's assume w1 and w2 are queued to a different wq q1 which has
  255. ``WQ_CPU_INTENSIVE`` set, ::
  256. TIME IN MSECS EVENT
  257. 0 w0 starts and burns CPU
  258. 5 w0 sleeps
  259. 5 w1 and w2 start and burn CPU
  260. 10 w1 sleeps
  261. 15 w2 sleeps
  262. 15 w0 wakes up and burns CPU
  263. 20 w0 finishes
  264. 20 w1 wakes up and finishes
  265. 25 w2 wakes up and finishes
  266. Guidelines
  267. ==========
  268. * Do not forget to use ``WQ_MEM_RECLAIM`` if a wq may process work
  269. items which are used during memory reclaim. Each wq with
  270. ``WQ_MEM_RECLAIM`` set has an execution context reserved for it. If
  271. there is dependency among multiple work items used during memory
  272. reclaim, they should be queued to separate wq each with
  273. ``WQ_MEM_RECLAIM``.
  274. * Unless strict ordering is required, there is no need to use ST wq.
  275. * Unless there is a specific need, using 0 for @max_active is
  276. recommended. In most use cases, concurrency level usually stays
  277. well under the default limit.
  278. * A wq serves as a domain for forward progress guarantee
  279. (``WQ_MEM_RECLAIM``, flush and work item attributes. Work items
  280. which are not involved in memory reclaim and don't need to be
  281. flushed as a part of a group of work items, and don't require any
  282. special attribute, can use one of the system wq. There is no
  283. difference in execution characteristics between using a dedicated wq
  284. and a system wq.
  285. * Unless work items are expected to consume a huge amount of CPU
  286. cycles, using a bound wq is usually beneficial due to the increased
  287. level of locality in wq operations and work item execution.
  288. Affinity Scopes
  289. ===============
  290. An unbound workqueue groups CPUs according to its affinity scope to improve
  291. cache locality. For example, if a workqueue is using the default affinity
  292. scope of "cache", it will group CPUs according to last level cache
  293. boundaries. A work item queued on the workqueue will be assigned to a worker
  294. on one of the CPUs which share the last level cache with the issuing CPU.
  295. Once started, the worker may or may not be allowed to move outside the scope
  296. depending on the ``affinity_strict`` setting of the scope.
  297. Workqueue currently supports the following affinity scopes.
  298. ``default``
  299. Use the scope in module parameter ``workqueue.default_affinity_scope``
  300. which is always set to one of the scopes below.
  301. ``cpu``
  302. CPUs are not grouped. A work item issued on one CPU is processed by a
  303. worker on the same CPU. This makes unbound workqueues behave as per-cpu
  304. workqueues without concurrency management.
  305. ``smt``
  306. CPUs are grouped according to SMT boundaries. This usually means that the
  307. logical threads of each physical CPU core are grouped together.
  308. ``cache``
  309. CPUs are grouped according to cache boundaries. Which specific cache
  310. boundary is used is determined by the arch code. L3 is used in a lot of
  311. cases. This is the default affinity scope.
  312. ``numa``
  313. CPUs are grouped according to NUMA boundaries.
  314. ``system``
  315. All CPUs are put in the same group. Workqueue makes no effort to process a
  316. work item on a CPU close to the issuing CPU.
  317. The default affinity scope can be changed with the module parameter
  318. ``workqueue.default_affinity_scope`` and a specific workqueue's affinity
  319. scope can be changed using ``apply_workqueue_attrs()``.
  320. If ``WQ_SYSFS`` is set, the workqueue will have the following affinity scope
  321. related interface files under its ``/sys/devices/virtual/workqueue/WQ_NAME/``
  322. directory.
  323. ``affinity_scope``
  324. Read to see the current affinity scope. Write to change.
  325. When default is the current scope, reading this file will also show the
  326. current effective scope in parentheses, for example, ``default (cache)``.
  327. ``affinity_strict``
  328. 0 by default indicating that affinity scopes are not strict. When a work
  329. item starts execution, workqueue makes a best-effort attempt to ensure
  330. that the worker is inside its affinity scope, which is called
  331. repatriation. Once started, the scheduler is free to move the worker
  332. anywhere in the system as it sees fit. This enables benefiting from scope
  333. locality while still being able to utilize other CPUs if necessary and
  334. available.
  335. If set to 1, all workers of the scope are guaranteed always to be in the
  336. scope. This may be useful when crossing affinity scopes has other
  337. implications, for example, in terms of power consumption or workload
  338. isolation. Strict NUMA scope can also be used to match the workqueue
  339. behavior of older kernels.
  340. Affinity Scopes and Performance
  341. ===============================
  342. It'd be ideal if an unbound workqueue's behavior is optimal for vast
  343. majority of use cases without further tuning. Unfortunately, in the current
  344. kernel, there exists a pronounced trade-off between locality and utilization
  345. necessitating explicit configurations when workqueues are heavily used.
  346. Higher locality leads to higher efficiency where more work is performed for
  347. the same number of consumed CPU cycles. However, higher locality may also
  348. cause lower overall system utilization if the work items are not spread
  349. enough across the affinity scopes by the issuers. The following performance
  350. testing with dm-crypt clearly illustrates this trade-off.
  351. The tests are run on a CPU with 12-cores/24-threads split across four L3
  352. caches (AMD Ryzen 9 3900x). CPU clock boost is turned off for consistency.
  353. ``/dev/dm-0`` is a dm-crypt device created on NVME SSD (Samsung 990 PRO) and
  354. opened with ``cryptsetup`` with default settings.
  355. Scenario 1: Enough issuers and work spread across the machine
  356. -------------------------------------------------------------
  357. The command used: ::
  358. $ fio --filename=/dev/dm-0 --direct=1 --rw=randrw --bs=32k --ioengine=libaio \
  359. --iodepth=64 --runtime=60 --numjobs=24 --time_based --group_reporting \
  360. --name=iops-test-job --verify=sha512
  361. There are 24 issuers, each issuing 64 IOs concurrently. ``--verify=sha512``
  362. makes ``fio`` generate and read back the content each time which makes
  363. execution locality matter between the issuer and ``kcryptd``. The following
  364. are the read bandwidths and CPU utilizations depending on different affinity
  365. scope settings on ``kcryptd`` measured over five runs. Bandwidths are in
  366. MiBps, and CPU util in percents.
  367. .. list-table::
  368. :widths: 16 20 20
  369. :header-rows: 1
  370. * - Affinity
  371. - Bandwidth (MiBps)
  372. - CPU util (%)
  373. * - system
  374. - 1159.40 ±1.34
  375. - 99.31 ±0.02
  376. * - cache
  377. - 1166.40 ±0.89
  378. - 99.34 ±0.01
  379. * - cache (strict)
  380. - 1166.00 ±0.71
  381. - 99.35 ±0.01
  382. With enough issuers spread across the system, there is no downside to
  383. "cache", strict or otherwise. All three configurations saturate the whole
  384. machine but the cache-affine ones outperform by 0.6% thanks to improved
  385. locality.
  386. Scenario 2: Fewer issuers, enough work for saturation
  387. -----------------------------------------------------
  388. The command used: ::
  389. $ fio --filename=/dev/dm-0 --direct=1 --rw=randrw --bs=32k \
  390. --ioengine=libaio --iodepth=64 --runtime=60 --numjobs=8 \
  391. --time_based --group_reporting --name=iops-test-job --verify=sha512
  392. The only difference from the previous scenario is ``--numjobs=8``. There are
  393. a third of the issuers but is still enough total work to saturate the
  394. system.
  395. .. list-table::
  396. :widths: 16 20 20
  397. :header-rows: 1
  398. * - Affinity
  399. - Bandwidth (MiBps)
  400. - CPU util (%)
  401. * - system
  402. - 1155.40 ±0.89
  403. - 97.41 ±0.05
  404. * - cache
  405. - 1154.40 ±1.14
  406. - 96.15 ±0.09
  407. * - cache (strict)
  408. - 1112.00 ±4.64
  409. - 93.26 ±0.35
  410. This is more than enough work to saturate the system. Both "system" and
  411. "cache" are nearly saturating the machine but not fully. "cache" is using
  412. less CPU but the better efficiency puts it at the same bandwidth as
  413. "system".
  414. Eight issuers moving around over four L3 cache scope still allow "cache
  415. (strict)" to mostly saturate the machine but the loss of work conservation
  416. is now starting to hurt with 3.7% bandwidth loss.
  417. Scenario 3: Even fewer issuers, not enough work to saturate
  418. -----------------------------------------------------------
  419. The command used: ::
  420. $ fio --filename=/dev/dm-0 --direct=1 --rw=randrw --bs=32k \
  421. --ioengine=libaio --iodepth=64 --runtime=60 --numjobs=4 \
  422. --time_based --group_reporting --name=iops-test-job --verify=sha512
  423. Again, the only difference is ``--numjobs=4``. With the number of issuers
  424. reduced to four, there now isn't enough work to saturate the whole system
  425. and the bandwidth becomes dependent on completion latencies.
  426. .. list-table::
  427. :widths: 16 20 20
  428. :header-rows: 1
  429. * - Affinity
  430. - Bandwidth (MiBps)
  431. - CPU util (%)
  432. * - system
  433. - 993.60 ±1.82
  434. - 75.49 ±0.06
  435. * - cache
  436. - 973.40 ±1.52
  437. - 74.90 ±0.07
  438. * - cache (strict)
  439. - 828.20 ±4.49
  440. - 66.84 ±0.29
  441. Now, the tradeoff between locality and utilization is clearer. "cache" shows
  442. 2% bandwidth loss compared to "system" and "cache (struct)" whopping 20%.
  443. Conclusion and Recommendations
  444. ------------------------------
  445. In the above experiments, the efficiency advantage of the "cache" affinity
  446. scope over "system" is, while consistent and noticeable, small. However, the
  447. impact is dependent on the distances between the scopes and may be more
  448. pronounced in processors with more complex topologies.
  449. While the loss of work-conservation in certain scenarios hurts, it is a lot
  450. better than "cache (strict)" and maximizing workqueue utilization is
  451. unlikely to be the common case anyway. As such, "cache" is the default
  452. affinity scope for unbound pools.
  453. * As there is no one option which is great for most cases, workqueue usages
  454. that may consume a significant amount of CPU are recommended to configure
  455. the workqueues using ``apply_workqueue_attrs()`` and/or enable
  456. ``WQ_SYSFS``.
  457. * An unbound workqueue with strict "cpu" affinity scope behaves the same as
  458. ``WQ_CPU_INTENSIVE`` per-cpu workqueue. There is no real advanage to the
  459. latter and an unbound workqueue provides a lot more flexibility.
  460. * Affinity scopes are introduced in Linux v6.5. To emulate the previous
  461. behavior, use strict "numa" affinity scope.
  462. * The loss of work-conservation in non-strict affinity scopes is likely
  463. originating from the scheduler. There is no theoretical reason why the
  464. kernel wouldn't be able to do the right thing and maintain
  465. work-conservation in most cases. As such, it is possible that future
  466. scheduler improvements may make most of these tunables unnecessary.
  467. Examining Configuration
  468. =======================
  469. Use tools/workqueue/wq_dump.py to examine unbound CPU affinity
  470. configuration, worker pools and how workqueues map to the pools: ::
  471. $ tools/workqueue/wq_dump.py
  472. Affinity Scopes
  473. ===============
  474. wq_unbound_cpumask=0000000f
  475. CPU
  476. nr_pods 4
  477. pod_cpus [0]=00000001 [1]=00000002 [2]=00000004 [3]=00000008
  478. pod_node [0]=0 [1]=0 [2]=1 [3]=1
  479. cpu_pod [0]=0 [1]=1 [2]=2 [3]=3
  480. SMT
  481. nr_pods 4
  482. pod_cpus [0]=00000001 [1]=00000002 [2]=00000004 [3]=00000008
  483. pod_node [0]=0 [1]=0 [2]=1 [3]=1
  484. cpu_pod [0]=0 [1]=1 [2]=2 [3]=3
  485. CACHE (default)
  486. nr_pods 2
  487. pod_cpus [0]=00000003 [1]=0000000c
  488. pod_node [0]=0 [1]=1
  489. cpu_pod [0]=0 [1]=0 [2]=1 [3]=1
  490. NUMA
  491. nr_pods 2
  492. pod_cpus [0]=00000003 [1]=0000000c
  493. pod_node [0]=0 [1]=1
  494. cpu_pod [0]=0 [1]=0 [2]=1 [3]=1
  495. SYSTEM
  496. nr_pods 1
  497. pod_cpus [0]=0000000f
  498. pod_node [0]=-1
  499. cpu_pod [0]=0 [1]=0 [2]=0 [3]=0
  500. Worker Pools
  501. ============
  502. pool[00] ref= 1 nice= 0 idle/workers= 4/ 4 cpu= 0
  503. pool[01] ref= 1 nice=-20 idle/workers= 2/ 2 cpu= 0
  504. pool[02] ref= 1 nice= 0 idle/workers= 4/ 4 cpu= 1
  505. pool[03] ref= 1 nice=-20 idle/workers= 2/ 2 cpu= 1
  506. pool[04] ref= 1 nice= 0 idle/workers= 4/ 4 cpu= 2
  507. pool[05] ref= 1 nice=-20 idle/workers= 2/ 2 cpu= 2
  508. pool[06] ref= 1 nice= 0 idle/workers= 3/ 3 cpu= 3
  509. pool[07] ref= 1 nice=-20 idle/workers= 2/ 2 cpu= 3
  510. pool[08] ref=42 nice= 0 idle/workers= 6/ 6 cpus=0000000f
  511. pool[09] ref=28 nice= 0 idle/workers= 3/ 3 cpus=00000003
  512. pool[10] ref=28 nice= 0 idle/workers= 17/ 17 cpus=0000000c
  513. pool[11] ref= 1 nice=-20 idle/workers= 1/ 1 cpus=0000000f
  514. pool[12] ref= 2 nice=-20 idle/workers= 1/ 1 cpus=00000003
  515. pool[13] ref= 2 nice=-20 idle/workers= 1/ 1 cpus=0000000c
  516. Workqueue CPU -> pool
  517. =====================
  518. [ workqueue \ CPU 0 1 2 3 dfl]
  519. events percpu 0 2 4 6
  520. events_highpri percpu 1 3 5 7
  521. events_long percpu 0 2 4 6
  522. events_unbound unbound 9 9 10 10 8
  523. events_freezable percpu 0 2 4 6
  524. events_power_efficient percpu 0 2 4 6
  525. events_freezable_pwr_ef percpu 0 2 4 6
  526. rcu_gp percpu 0 2 4 6
  527. rcu_par_gp percpu 0 2 4 6
  528. slub_flushwq percpu 0 2 4 6
  529. netns ordered 8 8 8 8 8
  530. ...
  531. See the command's help message for more info.
  532. Monitoring
  533. ==========
  534. Use tools/workqueue/wq_monitor.py to monitor workqueue operations: ::
  535. $ tools/workqueue/wq_monitor.py events
  536. total infl CPUtime CPUhog CMW/RPR mayday rescued
  537. events 18545 0 6.1 0 5 - -
  538. events_highpri 8 0 0.0 0 0 - -
  539. events_long 3 0 0.0 0 0 - -
  540. events_unbound 38306 0 0.1 - 7 - -
  541. events_freezable 0 0 0.0 0 0 - -
  542. events_power_efficient 29598 0 0.2 0 0 - -
  543. events_freezable_pwr_ef 10 0 0.0 0 0 - -
  544. sock_diag_events 0 0 0.0 0 0 - -
  545. total infl CPUtime CPUhog CMW/RPR mayday rescued
  546. events 18548 0 6.1 0 5 - -
  547. events_highpri 8 0 0.0 0 0 - -
  548. events_long 3 0 0.0 0 0 - -
  549. events_unbound 38322 0 0.1 - 7 - -
  550. events_freezable 0 0 0.0 0 0 - -
  551. events_power_efficient 29603 0 0.2 0 0 - -
  552. events_freezable_pwr_ef 10 0 0.0 0 0 - -
  553. sock_diag_events 0 0 0.0 0 0 - -
  554. ...
  555. See the command's help message for more info.
  556. Debugging
  557. =========
  558. Because the work functions are executed by generic worker threads
  559. there are a few tricks needed to shed some light on misbehaving
  560. workqueue users.
  561. Worker threads show up in the process list as: ::
  562. root 5671 0.0 0.0 0 0 ? S 12:07 0:00 [kworker/0:1]
  563. root 5672 0.0 0.0 0 0 ? S 12:07 0:00 [kworker/1:2]
  564. root 5673 0.0 0.0 0 0 ? S 12:12 0:00 [kworker/0:0]
  565. root 5674 0.0 0.0 0 0 ? S 12:13 0:00 [kworker/1:0]
  566. If kworkers are going crazy (using too much cpu), there are two types
  567. of possible problems:
  568. 1. Something being scheduled in rapid succession
  569. 2. A single work item that consumes lots of cpu cycles
  570. The first one can be tracked using tracing: ::
  571. $ echo workqueue:workqueue_queue_work > /sys/kernel/tracing/set_event
  572. $ cat /sys/kernel/tracing/trace_pipe > out.txt
  573. (wait a few secs)
  574. ^C
  575. If something is busy looping on work queueing, it would be dominating
  576. the output and the offender can be determined with the work item
  577. function.
  578. For the second type of problems it should be possible to just check
  579. the stack trace of the offending worker thread. ::
  580. $ cat /proc/THE_OFFENDING_KWORKER/stack
  581. The work item's function should be trivially visible in the stack
  582. trace.
  583. Non-reentrance Conditions
  584. =========================
  585. Workqueue guarantees that a work item cannot be re-entrant if the following
  586. conditions hold after a work item gets queued:
  587. 1. The work function hasn't been changed.
  588. 2. No one queues the work item to another workqueue.
  589. 3. The work item hasn't been reinitiated.
  590. In other words, if the above conditions hold, the work item is guaranteed to be
  591. executed by at most one worker system-wide at any given time.
  592. Note that requeuing the work item (to the same queue) in the self function
  593. doesn't break these conditions, so it's safe to do. Otherwise, caution is
  594. required when breaking the conditions inside a work function.
  595. Kernel Inline Documentations Reference
  596. ======================================
  597. .. kernel-doc:: include/linux/workqueue.h
  598. .. kernel-doc:: kernel/workqueue.c