io-wq.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Basic worker thread pool for io_uring
  4. *
  5. * Copyright (C) 2019 Jens Axboe
  6. *
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/errno.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/percpu.h>
  13. #include <linux/slab.h>
  14. #include <linux/rculist_nulls.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpuset.h>
  17. #include <linux/task_work.h>
  18. #include <linux/audit.h>
  19. #include <linux/mmu_context.h>
  20. #include <linux/sched/sysctl.h>
  21. #include <uapi/linux/io_uring.h>
  22. #include "io-wq.h"
  23. #include "slist.h"
  24. #include "io_uring.h"
  25. #define WORKER_IDLE_TIMEOUT (5 * HZ)
  26. #define WORKER_INIT_LIMIT 3
  27. enum {
  28. IO_WORKER_F_UP = 0, /* up and active */
  29. IO_WORKER_F_RUNNING = 1, /* account as running */
  30. IO_WORKER_F_FREE = 2, /* worker on free list */
  31. };
  32. enum {
  33. IO_WQ_BIT_EXIT = 0, /* wq exiting */
  34. IO_WQ_BIT_EXIT_ON_IDLE = 1, /* allow all workers to exit on idle */
  35. };
  36. enum {
  37. IO_ACCT_STALLED_BIT = 0, /* stalled on hash */
  38. };
  39. /*
  40. * One for each thread in a wq pool
  41. */
  42. struct io_worker {
  43. refcount_t ref;
  44. unsigned long flags;
  45. struct hlist_nulls_node nulls_node;
  46. struct list_head all_list;
  47. struct task_struct *task;
  48. struct io_wq *wq;
  49. struct io_wq_acct *acct;
  50. struct io_wq_work *cur_work;
  51. raw_spinlock_t lock;
  52. struct completion ref_done;
  53. unsigned long create_state;
  54. struct callback_head create_work;
  55. int init_retries;
  56. union {
  57. struct rcu_head rcu;
  58. struct delayed_work work;
  59. };
  60. };
  61. #if BITS_PER_LONG == 64
  62. #define IO_WQ_HASH_ORDER 6
  63. #else
  64. #define IO_WQ_HASH_ORDER 5
  65. #endif
  66. #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER)
  67. struct io_wq_acct {
  68. /**
  69. * Protects access to the worker lists.
  70. */
  71. raw_spinlock_t workers_lock;
  72. unsigned nr_workers;
  73. unsigned max_workers;
  74. atomic_t nr_running;
  75. /**
  76. * The list of free workers. Protected by #workers_lock
  77. * (write) and RCU (read).
  78. */
  79. struct hlist_nulls_head free_list;
  80. /**
  81. * The list of all workers. Protected by #workers_lock
  82. * (write) and RCU (read).
  83. */
  84. struct list_head all_list;
  85. raw_spinlock_t lock;
  86. struct io_wq_work_list work_list;
  87. unsigned long flags;
  88. };
  89. enum {
  90. IO_WQ_ACCT_BOUND,
  91. IO_WQ_ACCT_UNBOUND,
  92. IO_WQ_ACCT_NR,
  93. };
  94. /*
  95. * Per io_wq state
  96. */
  97. struct io_wq {
  98. unsigned long state;
  99. struct io_wq_hash *hash;
  100. atomic_t worker_refs;
  101. struct completion worker_done;
  102. struct hlist_node cpuhp_node;
  103. struct task_struct *task;
  104. struct io_wq_acct acct[IO_WQ_ACCT_NR];
  105. struct wait_queue_entry wait;
  106. struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
  107. cpumask_var_t cpu_mask;
  108. };
  109. static enum cpuhp_state io_wq_online;
  110. struct io_cb_cancel_data {
  111. work_cancel_fn *fn;
  112. void *data;
  113. int nr_running;
  114. int nr_pending;
  115. bool cancel_all;
  116. };
  117. static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct);
  118. static void io_wq_dec_running(struct io_worker *worker);
  119. static bool io_acct_cancel_pending_work(struct io_wq *wq,
  120. struct io_wq_acct *acct,
  121. struct io_cb_cancel_data *match);
  122. static void create_worker_cb(struct callback_head *cb);
  123. static void io_wq_cancel_tw_create(struct io_wq *wq);
  124. static inline unsigned int __io_get_work_hash(unsigned int work_flags)
  125. {
  126. return work_flags >> IO_WQ_HASH_SHIFT;
  127. }
  128. static inline unsigned int io_get_work_hash(struct io_wq_work *work)
  129. {
  130. return __io_get_work_hash(atomic_read(&work->flags));
  131. }
  132. static bool io_worker_get(struct io_worker *worker)
  133. {
  134. return refcount_inc_not_zero(&worker->ref);
  135. }
  136. static void io_worker_release(struct io_worker *worker)
  137. {
  138. if (refcount_dec_and_test(&worker->ref))
  139. complete(&worker->ref_done);
  140. }
  141. static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound)
  142. {
  143. return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
  144. }
  145. static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq,
  146. unsigned int work_flags)
  147. {
  148. return io_get_acct(wq, !(work_flags & IO_WQ_WORK_UNBOUND));
  149. }
  150. static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker)
  151. {
  152. return worker->acct;
  153. }
  154. static void io_worker_ref_put(struct io_wq *wq)
  155. {
  156. if (atomic_dec_and_test(&wq->worker_refs))
  157. complete(&wq->worker_done);
  158. }
  159. bool io_wq_worker_stopped(void)
  160. {
  161. struct io_worker *worker = current->worker_private;
  162. if (WARN_ON_ONCE(!io_wq_current_is_worker()))
  163. return true;
  164. return test_bit(IO_WQ_BIT_EXIT, &worker->wq->state);
  165. }
  166. static void io_worker_cancel_cb(struct io_worker *worker)
  167. {
  168. struct io_wq_acct *acct = io_wq_get_acct(worker);
  169. struct io_wq *wq = worker->wq;
  170. atomic_dec(&acct->nr_running);
  171. raw_spin_lock(&acct->workers_lock);
  172. acct->nr_workers--;
  173. raw_spin_unlock(&acct->workers_lock);
  174. io_worker_ref_put(wq);
  175. clear_bit_unlock(0, &worker->create_state);
  176. io_worker_release(worker);
  177. }
  178. static bool io_task_worker_match(struct callback_head *cb, void *data)
  179. {
  180. struct io_worker *worker;
  181. if (cb->func != create_worker_cb)
  182. return false;
  183. worker = container_of(cb, struct io_worker, create_work);
  184. return worker == data;
  185. }
  186. static void io_worker_exit(struct io_worker *worker)
  187. {
  188. struct io_wq *wq = worker->wq;
  189. struct io_wq_acct *acct = io_wq_get_acct(worker);
  190. while (1) {
  191. struct callback_head *cb = task_work_cancel_match(wq->task,
  192. io_task_worker_match, worker);
  193. if (!cb)
  194. break;
  195. io_worker_cancel_cb(worker);
  196. }
  197. io_worker_release(worker);
  198. wait_for_completion(&worker->ref_done);
  199. raw_spin_lock(&acct->workers_lock);
  200. if (test_bit(IO_WORKER_F_FREE, &worker->flags))
  201. hlist_nulls_del_rcu(&worker->nulls_node);
  202. list_del_rcu(&worker->all_list);
  203. raw_spin_unlock(&acct->workers_lock);
  204. io_wq_dec_running(worker);
  205. /*
  206. * this worker is a goner, clear ->worker_private to avoid any
  207. * inc/dec running calls that could happen as part of exit from
  208. * touching 'worker'.
  209. */
  210. current->worker_private = NULL;
  211. kfree_rcu(worker, rcu);
  212. io_worker_ref_put(wq);
  213. do_exit(0);
  214. }
  215. static inline bool __io_acct_run_queue(struct io_wq_acct *acct)
  216. {
  217. return !test_bit(IO_ACCT_STALLED_BIT, &acct->flags) &&
  218. !wq_list_empty(&acct->work_list);
  219. }
  220. /*
  221. * If there's work to do, returns true with acct->lock acquired. If not,
  222. * returns false with no lock held.
  223. */
  224. static inline bool io_acct_run_queue(struct io_wq_acct *acct)
  225. __acquires(&acct->lock)
  226. {
  227. raw_spin_lock(&acct->lock);
  228. if (__io_acct_run_queue(acct))
  229. return true;
  230. raw_spin_unlock(&acct->lock);
  231. return false;
  232. }
  233. /*
  234. * Check head of free list for an available worker. If one isn't available,
  235. * caller must create one.
  236. */
  237. static bool io_acct_activate_free_worker(struct io_wq_acct *acct)
  238. __must_hold(RCU)
  239. {
  240. struct hlist_nulls_node *n;
  241. struct io_worker *worker;
  242. /*
  243. * Iterate free_list and see if we can find an idle worker to
  244. * activate. If a given worker is on the free_list but in the process
  245. * of exiting, keep trying.
  246. */
  247. hlist_nulls_for_each_entry_rcu(worker, n, &acct->free_list, nulls_node) {
  248. if (!io_worker_get(worker))
  249. continue;
  250. /*
  251. * If the worker is already running, it's either already
  252. * starting work or finishing work. In either case, if it does
  253. * to go sleep, we'll kick off a new task for this work anyway.
  254. */
  255. wake_up_process(worker->task);
  256. io_worker_release(worker);
  257. return true;
  258. }
  259. return false;
  260. }
  261. /*
  262. * We need a worker. If we find a free one, we're good. If not, and we're
  263. * below the max number of workers, create one.
  264. */
  265. static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct)
  266. {
  267. /*
  268. * Most likely an attempt to queue unbounded work on an io_wq that
  269. * wasn't setup with any unbounded workers.
  270. */
  271. if (unlikely(!acct->max_workers))
  272. pr_warn_once("io-wq is not configured for unbound workers");
  273. raw_spin_lock(&acct->workers_lock);
  274. if (acct->nr_workers >= acct->max_workers) {
  275. raw_spin_unlock(&acct->workers_lock);
  276. return true;
  277. }
  278. acct->nr_workers++;
  279. raw_spin_unlock(&acct->workers_lock);
  280. atomic_inc(&acct->nr_running);
  281. atomic_inc(&wq->worker_refs);
  282. return create_io_worker(wq, acct);
  283. }
  284. static void io_wq_inc_running(struct io_worker *worker)
  285. {
  286. struct io_wq_acct *acct = io_wq_get_acct(worker);
  287. atomic_inc(&acct->nr_running);
  288. }
  289. static void create_worker_cb(struct callback_head *cb)
  290. {
  291. struct io_worker *worker;
  292. struct io_wq *wq;
  293. struct io_wq_acct *acct;
  294. bool activated_free_worker, do_create = false;
  295. worker = container_of(cb, struct io_worker, create_work);
  296. wq = worker->wq;
  297. acct = worker->acct;
  298. rcu_read_lock();
  299. activated_free_worker = io_acct_activate_free_worker(acct);
  300. rcu_read_unlock();
  301. if (activated_free_worker)
  302. goto no_need_create;
  303. raw_spin_lock(&acct->workers_lock);
  304. if (acct->nr_workers < acct->max_workers) {
  305. acct->nr_workers++;
  306. do_create = true;
  307. }
  308. raw_spin_unlock(&acct->workers_lock);
  309. if (do_create) {
  310. create_io_worker(wq, acct);
  311. } else {
  312. no_need_create:
  313. atomic_dec(&acct->nr_running);
  314. io_worker_ref_put(wq);
  315. }
  316. clear_bit_unlock(0, &worker->create_state);
  317. io_worker_release(worker);
  318. }
  319. static bool io_queue_worker_create(struct io_worker *worker,
  320. struct io_wq_acct *acct,
  321. task_work_func_t func)
  322. {
  323. struct io_wq *wq = worker->wq;
  324. /* raced with exit, just ignore create call */
  325. if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
  326. goto fail;
  327. if (!io_worker_get(worker))
  328. goto fail;
  329. /*
  330. * create_state manages ownership of create_work/index. We should
  331. * only need one entry per worker, as the worker going to sleep
  332. * will trigger the condition, and waking will clear it once it
  333. * runs the task_work.
  334. */
  335. if (test_bit(0, &worker->create_state) ||
  336. test_and_set_bit_lock(0, &worker->create_state))
  337. goto fail_release;
  338. atomic_inc(&wq->worker_refs);
  339. init_task_work(&worker->create_work, func);
  340. if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
  341. /*
  342. * EXIT may have been set after checking it above, check after
  343. * adding the task_work and remove any creation item if it is
  344. * now set. wq exit does that too, but we can have added this
  345. * work item after we canceled in io_wq_exit_workers().
  346. */
  347. if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
  348. io_wq_cancel_tw_create(wq);
  349. io_worker_ref_put(wq);
  350. return true;
  351. }
  352. io_worker_ref_put(wq);
  353. clear_bit_unlock(0, &worker->create_state);
  354. fail_release:
  355. io_worker_release(worker);
  356. fail:
  357. atomic_dec(&acct->nr_running);
  358. io_worker_ref_put(wq);
  359. return false;
  360. }
  361. /* Defer if current and next work are both hashed to the same chain */
  362. static bool io_wq_hash_defer(struct io_wq_work *work, struct io_wq_acct *acct)
  363. {
  364. unsigned int hash, work_flags;
  365. struct io_wq_work *next;
  366. lockdep_assert_held(&acct->lock);
  367. work_flags = atomic_read(&work->flags);
  368. if (!__io_wq_is_hashed(work_flags))
  369. return false;
  370. /* should not happen, io_acct_run_queue() said we had work */
  371. if (wq_list_empty(&acct->work_list))
  372. return true;
  373. hash = __io_get_work_hash(work_flags);
  374. next = container_of(acct->work_list.first, struct io_wq_work, list);
  375. work_flags = atomic_read(&next->flags);
  376. if (!__io_wq_is_hashed(work_flags))
  377. return false;
  378. return hash == __io_get_work_hash(work_flags);
  379. }
  380. static void io_wq_dec_running(struct io_worker *worker)
  381. {
  382. struct io_wq_acct *acct = io_wq_get_acct(worker);
  383. struct io_wq *wq = worker->wq;
  384. if (!test_bit(IO_WORKER_F_UP, &worker->flags))
  385. return;
  386. if (!atomic_dec_and_test(&acct->nr_running))
  387. return;
  388. if (!worker->cur_work)
  389. return;
  390. if (!io_acct_run_queue(acct))
  391. return;
  392. if (io_wq_hash_defer(worker->cur_work, acct)) {
  393. raw_spin_unlock(&acct->lock);
  394. return;
  395. }
  396. raw_spin_unlock(&acct->lock);
  397. atomic_inc(&acct->nr_running);
  398. atomic_inc(&wq->worker_refs);
  399. io_queue_worker_create(worker, acct, create_worker_cb);
  400. }
  401. /*
  402. * Worker will start processing some work. Move it to the busy list, if
  403. * it's currently on the freelist
  404. */
  405. static void __io_worker_busy(struct io_wq_acct *acct, struct io_worker *worker)
  406. {
  407. if (test_bit(IO_WORKER_F_FREE, &worker->flags)) {
  408. clear_bit(IO_WORKER_F_FREE, &worker->flags);
  409. raw_spin_lock(&acct->workers_lock);
  410. hlist_nulls_del_init_rcu(&worker->nulls_node);
  411. raw_spin_unlock(&acct->workers_lock);
  412. }
  413. }
  414. /*
  415. * No work, worker going to sleep. Move to freelist.
  416. */
  417. static void __io_worker_idle(struct io_wq_acct *acct, struct io_worker *worker)
  418. __must_hold(acct->workers_lock)
  419. {
  420. if (!test_bit(IO_WORKER_F_FREE, &worker->flags)) {
  421. set_bit(IO_WORKER_F_FREE, &worker->flags);
  422. hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list);
  423. }
  424. }
  425. static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
  426. {
  427. bool ret = false;
  428. spin_lock_irq(&wq->hash->wait.lock);
  429. if (list_empty(&wq->wait.entry)) {
  430. __add_wait_queue(&wq->hash->wait, &wq->wait);
  431. if (!test_bit(hash, &wq->hash->map)) {
  432. __set_current_state(TASK_RUNNING);
  433. list_del_init(&wq->wait.entry);
  434. ret = true;
  435. }
  436. }
  437. spin_unlock_irq(&wq->hash->wait.lock);
  438. return ret;
  439. }
  440. static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct,
  441. struct io_wq *wq)
  442. __must_hold(acct->lock)
  443. {
  444. struct io_wq_work_node *node, *prev;
  445. struct io_wq_work *work, *tail;
  446. unsigned int stall_hash = -1U;
  447. wq_list_for_each(node, prev, &acct->work_list) {
  448. unsigned int work_flags;
  449. unsigned int hash;
  450. work = container_of(node, struct io_wq_work, list);
  451. /* not hashed, can run anytime */
  452. work_flags = atomic_read(&work->flags);
  453. if (!__io_wq_is_hashed(work_flags)) {
  454. wq_list_del(&acct->work_list, node, prev);
  455. return work;
  456. }
  457. hash = __io_get_work_hash(work_flags);
  458. /* all items with this hash lie in [work, tail] */
  459. tail = wq->hash_tail[hash];
  460. /* hashed, can run if not already running */
  461. if (!test_and_set_bit(hash, &wq->hash->map)) {
  462. wq->hash_tail[hash] = NULL;
  463. wq_list_cut(&acct->work_list, &tail->list, prev);
  464. return work;
  465. }
  466. if (stall_hash == -1U)
  467. stall_hash = hash;
  468. /* fast forward to a next hash, for-each will fix up @prev */
  469. node = &tail->list;
  470. }
  471. if (stall_hash != -1U) {
  472. bool unstalled;
  473. /*
  474. * Set this before dropping the lock to avoid racing with new
  475. * work being added and clearing the stalled bit.
  476. */
  477. set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
  478. raw_spin_unlock(&acct->lock);
  479. unstalled = io_wait_on_hash(wq, stall_hash);
  480. raw_spin_lock(&acct->lock);
  481. if (unstalled) {
  482. clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
  483. if (wq_has_sleeper(&wq->hash->wait))
  484. wake_up(&wq->hash->wait);
  485. }
  486. }
  487. return NULL;
  488. }
  489. static void io_assign_current_work(struct io_worker *worker,
  490. struct io_wq_work *work)
  491. {
  492. if (work) {
  493. io_run_task_work();
  494. cond_resched();
  495. }
  496. raw_spin_lock(&worker->lock);
  497. worker->cur_work = work;
  498. raw_spin_unlock(&worker->lock);
  499. }
  500. /*
  501. * Called with acct->lock held, drops it before returning
  502. */
  503. static void io_worker_handle_work(struct io_wq_acct *acct,
  504. struct io_worker *worker)
  505. __releases(&acct->lock)
  506. {
  507. struct io_wq *wq = worker->wq;
  508. do {
  509. bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
  510. struct io_wq_work *work;
  511. /*
  512. * If we got some work, mark us as busy. If we didn't, but
  513. * the list isn't empty, it means we stalled on hashed work.
  514. * Mark us stalled so we don't keep looking for work when we
  515. * can't make progress, any work completion or insertion will
  516. * clear the stalled flag.
  517. */
  518. work = io_get_next_work(acct, wq);
  519. if (work) {
  520. /*
  521. * Make sure cancelation can find this, even before
  522. * it becomes the active work. That avoids a window
  523. * where the work has been removed from our general
  524. * work list, but isn't yet discoverable as the
  525. * current work item for this worker.
  526. */
  527. raw_spin_lock(&worker->lock);
  528. worker->cur_work = work;
  529. raw_spin_unlock(&worker->lock);
  530. }
  531. raw_spin_unlock(&acct->lock);
  532. if (!work)
  533. break;
  534. __io_worker_busy(acct, worker);
  535. io_assign_current_work(worker, work);
  536. __set_current_state(TASK_RUNNING);
  537. /* handle a whole dependent link */
  538. do {
  539. struct io_wq_work *next_hashed, *linked;
  540. unsigned int work_flags = atomic_read(&work->flags);
  541. unsigned int hash = __io_wq_is_hashed(work_flags)
  542. ? __io_get_work_hash(work_flags)
  543. : -1U;
  544. next_hashed = wq_next_work(work);
  545. if (do_kill &&
  546. (work_flags & IO_WQ_WORK_UNBOUND))
  547. atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
  548. io_wq_submit_work(work);
  549. io_assign_current_work(worker, NULL);
  550. linked = io_wq_free_work(work);
  551. work = next_hashed;
  552. if (!work && linked && !io_wq_is_hashed(linked)) {
  553. work = linked;
  554. linked = NULL;
  555. }
  556. io_assign_current_work(worker, work);
  557. if (linked)
  558. io_wq_enqueue(wq, linked);
  559. if (hash != -1U && !next_hashed) {
  560. /* serialize hash clear with wake_up() */
  561. spin_lock_irq(&wq->hash->wait.lock);
  562. clear_bit(hash, &wq->hash->map);
  563. clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
  564. spin_unlock_irq(&wq->hash->wait.lock);
  565. if (wq_has_sleeper(&wq->hash->wait))
  566. wake_up(&wq->hash->wait);
  567. }
  568. } while (work);
  569. if (!__io_acct_run_queue(acct))
  570. break;
  571. raw_spin_lock(&acct->lock);
  572. } while (1);
  573. }
  574. static int io_wq_worker(void *data)
  575. {
  576. struct io_worker *worker = data;
  577. struct io_wq_acct *acct = io_wq_get_acct(worker);
  578. struct io_wq *wq = worker->wq;
  579. bool exit_mask = false, last_timeout = false;
  580. char buf[TASK_COMM_LEN] = {};
  581. set_mask_bits(&worker->flags, 0,
  582. BIT(IO_WORKER_F_UP) | BIT(IO_WORKER_F_RUNNING));
  583. snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
  584. set_task_comm(current, buf);
  585. while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
  586. long ret;
  587. set_current_state(TASK_INTERRUPTIBLE);
  588. /*
  589. * If we have work to do, io_acct_run_queue() returns with
  590. * the acct->lock held. If not, it will drop it.
  591. */
  592. while (io_acct_run_queue(acct))
  593. io_worker_handle_work(acct, worker);
  594. raw_spin_lock(&acct->workers_lock);
  595. /*
  596. * Last sleep timed out. Exit if we're not the last worker,
  597. * or if someone modified our affinity. If wq is marked
  598. * idle-exit, drop the worker as well. This is used to avoid
  599. * keeping io-wq workers around for tasks that no longer have
  600. * any active io_uring instances.
  601. */
  602. if ((last_timeout && (exit_mask || acct->nr_workers > 1)) ||
  603. test_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state)) {
  604. acct->nr_workers--;
  605. raw_spin_unlock(&acct->workers_lock);
  606. __set_current_state(TASK_RUNNING);
  607. break;
  608. }
  609. last_timeout = false;
  610. __io_worker_idle(acct, worker);
  611. raw_spin_unlock(&acct->workers_lock);
  612. if (io_run_task_work())
  613. continue;
  614. ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
  615. if (signal_pending(current)) {
  616. struct ksignal ksig;
  617. if (!get_signal(&ksig))
  618. continue;
  619. break;
  620. }
  621. if (!ret) {
  622. last_timeout = true;
  623. exit_mask = !cpumask_test_cpu(raw_smp_processor_id(),
  624. wq->cpu_mask);
  625. }
  626. }
  627. if (test_bit(IO_WQ_BIT_EXIT, &wq->state) && io_acct_run_queue(acct))
  628. io_worker_handle_work(acct, worker);
  629. io_worker_exit(worker);
  630. return 0;
  631. }
  632. /*
  633. * Called when a worker is scheduled in. Mark us as currently running.
  634. */
  635. void io_wq_worker_running(struct task_struct *tsk)
  636. {
  637. struct io_worker *worker = tsk->worker_private;
  638. if (!worker)
  639. return;
  640. if (!test_bit(IO_WORKER_F_UP, &worker->flags))
  641. return;
  642. if (test_bit(IO_WORKER_F_RUNNING, &worker->flags))
  643. return;
  644. set_bit(IO_WORKER_F_RUNNING, &worker->flags);
  645. io_wq_inc_running(worker);
  646. }
  647. /*
  648. * Called when worker is going to sleep. If there are no workers currently
  649. * running and we have work pending, wake up a free one or create a new one.
  650. */
  651. void io_wq_worker_sleeping(struct task_struct *tsk)
  652. {
  653. struct io_worker *worker = tsk->worker_private;
  654. if (!worker)
  655. return;
  656. if (!test_bit(IO_WORKER_F_UP, &worker->flags))
  657. return;
  658. if (!test_bit(IO_WORKER_F_RUNNING, &worker->flags))
  659. return;
  660. clear_bit(IO_WORKER_F_RUNNING, &worker->flags);
  661. io_wq_dec_running(worker);
  662. }
  663. static void io_init_new_worker(struct io_wq *wq, struct io_wq_acct *acct, struct io_worker *worker,
  664. struct task_struct *tsk)
  665. {
  666. tsk->worker_private = worker;
  667. worker->task = tsk;
  668. set_cpus_allowed_ptr(tsk, wq->cpu_mask);
  669. raw_spin_lock(&acct->workers_lock);
  670. hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list);
  671. list_add_tail_rcu(&worker->all_list, &acct->all_list);
  672. set_bit(IO_WORKER_F_FREE, &worker->flags);
  673. raw_spin_unlock(&acct->workers_lock);
  674. wake_up_new_task(tsk);
  675. }
  676. static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
  677. {
  678. return true;
  679. }
  680. static inline bool io_should_retry_thread(struct io_worker *worker, long err)
  681. {
  682. /*
  683. * Prevent perpetual task_work retry, if the task (or its group) is
  684. * exiting.
  685. */
  686. if (fatal_signal_pending(current))
  687. return false;
  688. worker->init_retries++;
  689. switch (err) {
  690. case -EAGAIN:
  691. return worker->init_retries <= WORKER_INIT_LIMIT;
  692. /* Analogous to a fork() syscall, always retry on a restartable error */
  693. case -ERESTARTSYS:
  694. case -ERESTARTNOINTR:
  695. case -ERESTARTNOHAND:
  696. return true;
  697. default:
  698. return false;
  699. }
  700. }
  701. static void queue_create_worker_retry(struct io_worker *worker)
  702. {
  703. /*
  704. * We only bother retrying because there's a chance that the
  705. * failure to create a worker is due to some temporary condition
  706. * in the forking task (e.g. outstanding signal); give the task
  707. * some time to clear that condition.
  708. */
  709. schedule_delayed_work(&worker->work,
  710. msecs_to_jiffies(worker->init_retries * 5));
  711. }
  712. static void create_worker_cont(struct callback_head *cb)
  713. {
  714. struct io_worker *worker;
  715. struct task_struct *tsk;
  716. struct io_wq *wq;
  717. struct io_wq_acct *acct;
  718. worker = container_of(cb, struct io_worker, create_work);
  719. clear_bit_unlock(0, &worker->create_state);
  720. wq = worker->wq;
  721. acct = io_wq_get_acct(worker);
  722. tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
  723. if (!IS_ERR(tsk)) {
  724. io_init_new_worker(wq, acct, worker, tsk);
  725. io_worker_release(worker);
  726. return;
  727. } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
  728. atomic_dec(&acct->nr_running);
  729. raw_spin_lock(&acct->workers_lock);
  730. acct->nr_workers--;
  731. if (!acct->nr_workers) {
  732. struct io_cb_cancel_data match = {
  733. .fn = io_wq_work_match_all,
  734. .cancel_all = true,
  735. };
  736. raw_spin_unlock(&acct->workers_lock);
  737. while (io_acct_cancel_pending_work(wq, acct, &match))
  738. ;
  739. } else {
  740. raw_spin_unlock(&acct->workers_lock);
  741. }
  742. io_worker_ref_put(wq);
  743. kfree(worker);
  744. return;
  745. }
  746. /* re-create attempts grab a new worker ref, drop the existing one */
  747. io_worker_release(worker);
  748. queue_create_worker_retry(worker);
  749. }
  750. static void io_workqueue_create(struct work_struct *work)
  751. {
  752. struct io_worker *worker = container_of(work, struct io_worker,
  753. work.work);
  754. struct io_wq_acct *acct = io_wq_get_acct(worker);
  755. if (!io_queue_worker_create(worker, acct, create_worker_cont))
  756. kfree(worker);
  757. }
  758. static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct)
  759. {
  760. struct io_worker *worker;
  761. struct task_struct *tsk;
  762. __set_current_state(TASK_RUNNING);
  763. worker = kzalloc_obj(*worker);
  764. if (!worker) {
  765. fail:
  766. atomic_dec(&acct->nr_running);
  767. raw_spin_lock(&acct->workers_lock);
  768. acct->nr_workers--;
  769. raw_spin_unlock(&acct->workers_lock);
  770. io_worker_ref_put(wq);
  771. return false;
  772. }
  773. refcount_set(&worker->ref, 1);
  774. worker->wq = wq;
  775. worker->acct = acct;
  776. raw_spin_lock_init(&worker->lock);
  777. init_completion(&worker->ref_done);
  778. tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE);
  779. if (!IS_ERR(tsk)) {
  780. io_init_new_worker(wq, acct, worker, tsk);
  781. } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) {
  782. kfree(worker);
  783. goto fail;
  784. } else {
  785. INIT_DELAYED_WORK(&worker->work, io_workqueue_create);
  786. queue_create_worker_retry(worker);
  787. }
  788. return true;
  789. }
  790. /*
  791. * Iterate the passed in list and call the specific function for each
  792. * worker that isn't exiting
  793. */
  794. static bool io_acct_for_each_worker(struct io_wq_acct *acct,
  795. bool (*func)(struct io_worker *, void *),
  796. void *data)
  797. {
  798. struct io_worker *worker;
  799. bool ret = false;
  800. list_for_each_entry_rcu(worker, &acct->all_list, all_list) {
  801. if (io_worker_get(worker)) {
  802. /* no task if node is/was offline */
  803. if (worker->task)
  804. ret = func(worker, data);
  805. io_worker_release(worker);
  806. if (ret)
  807. break;
  808. }
  809. }
  810. return ret;
  811. }
  812. static void io_wq_for_each_worker(struct io_wq *wq,
  813. bool (*func)(struct io_worker *, void *),
  814. void *data)
  815. {
  816. for (int i = 0; i < IO_WQ_ACCT_NR; i++)
  817. if (io_acct_for_each_worker(&wq->acct[i], func, data))
  818. break;
  819. }
  820. static bool io_wq_worker_wake(struct io_worker *worker, void *data)
  821. {
  822. __set_notify_signal(worker->task);
  823. wake_up_process(worker->task);
  824. return false;
  825. }
  826. void io_wq_set_exit_on_idle(struct io_wq *wq, bool enable)
  827. {
  828. if (!wq->task)
  829. return;
  830. if (!enable) {
  831. clear_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state);
  832. return;
  833. }
  834. if (test_and_set_bit(IO_WQ_BIT_EXIT_ON_IDLE, &wq->state))
  835. return;
  836. rcu_read_lock();
  837. io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
  838. rcu_read_unlock();
  839. }
  840. static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
  841. {
  842. do {
  843. atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
  844. io_wq_submit_work(work);
  845. work = io_wq_free_work(work);
  846. } while (work);
  847. }
  848. static void io_wq_insert_work(struct io_wq *wq, struct io_wq_acct *acct,
  849. struct io_wq_work *work, unsigned int work_flags)
  850. {
  851. unsigned int hash;
  852. struct io_wq_work *tail;
  853. if (!__io_wq_is_hashed(work_flags)) {
  854. append:
  855. wq_list_add_tail(&work->list, &acct->work_list);
  856. return;
  857. }
  858. hash = __io_get_work_hash(work_flags);
  859. tail = wq->hash_tail[hash];
  860. wq->hash_tail[hash] = work;
  861. if (!tail)
  862. goto append;
  863. wq_list_add_after(&work->list, &tail->list, &acct->work_list);
  864. }
  865. static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
  866. {
  867. return work == data;
  868. }
  869. void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
  870. {
  871. unsigned int work_flags = atomic_read(&work->flags);
  872. struct io_wq_acct *acct = io_work_get_acct(wq, work_flags);
  873. struct io_cb_cancel_data match = {
  874. .fn = io_wq_work_match_item,
  875. .data = work,
  876. .cancel_all = false,
  877. };
  878. bool do_create;
  879. /*
  880. * If io-wq is exiting for this task, or if the request has explicitly
  881. * been marked as one that should not get executed, cancel it here.
  882. */
  883. if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
  884. (work_flags & IO_WQ_WORK_CANCEL)) {
  885. io_run_cancel(work, wq);
  886. return;
  887. }
  888. raw_spin_lock(&acct->lock);
  889. io_wq_insert_work(wq, acct, work, work_flags);
  890. clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
  891. raw_spin_unlock(&acct->lock);
  892. rcu_read_lock();
  893. do_create = !io_acct_activate_free_worker(acct);
  894. rcu_read_unlock();
  895. if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
  896. !atomic_read(&acct->nr_running))) {
  897. bool did_create;
  898. did_create = io_wq_create_worker(wq, acct);
  899. if (likely(did_create))
  900. return;
  901. raw_spin_lock(&acct->workers_lock);
  902. if (acct->nr_workers) {
  903. raw_spin_unlock(&acct->workers_lock);
  904. return;
  905. }
  906. raw_spin_unlock(&acct->workers_lock);
  907. /* fatal condition, failed to create the first worker */
  908. io_acct_cancel_pending_work(wq, acct, &match);
  909. }
  910. }
  911. /*
  912. * Work items that hash to the same value will not be done in parallel.
  913. * Used to limit concurrent writes, generally hashed by inode.
  914. */
  915. void io_wq_hash_work(struct io_wq_work *work, void *val)
  916. {
  917. unsigned int bit;
  918. bit = hash_ptr(val, IO_WQ_HASH_ORDER);
  919. atomic_or(IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT), &work->flags);
  920. }
  921. static bool __io_wq_worker_cancel(struct io_worker *worker,
  922. struct io_cb_cancel_data *match,
  923. struct io_wq_work *work)
  924. {
  925. if (work && match->fn(work, match->data)) {
  926. atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
  927. __set_notify_signal(worker->task);
  928. return true;
  929. }
  930. return false;
  931. }
  932. static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
  933. {
  934. struct io_cb_cancel_data *match = data;
  935. /*
  936. * Hold the lock to avoid ->cur_work going out of scope, caller
  937. * may dereference the passed in work.
  938. */
  939. raw_spin_lock(&worker->lock);
  940. if (__io_wq_worker_cancel(worker, match, worker->cur_work))
  941. match->nr_running++;
  942. raw_spin_unlock(&worker->lock);
  943. return match->nr_running && !match->cancel_all;
  944. }
  945. static inline void io_wq_remove_pending(struct io_wq *wq,
  946. struct io_wq_acct *acct,
  947. struct io_wq_work *work,
  948. struct io_wq_work_node *prev)
  949. {
  950. unsigned int hash = io_get_work_hash(work);
  951. struct io_wq_work *prev_work = NULL;
  952. if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) {
  953. if (prev)
  954. prev_work = container_of(prev, struct io_wq_work, list);
  955. if (prev_work && io_get_work_hash(prev_work) == hash)
  956. wq->hash_tail[hash] = prev_work;
  957. else
  958. wq->hash_tail[hash] = NULL;
  959. }
  960. wq_list_del(&acct->work_list, &work->list, prev);
  961. }
  962. static bool io_acct_cancel_pending_work(struct io_wq *wq,
  963. struct io_wq_acct *acct,
  964. struct io_cb_cancel_data *match)
  965. {
  966. struct io_wq_work_node *node, *prev;
  967. struct io_wq_work *work;
  968. raw_spin_lock(&acct->lock);
  969. wq_list_for_each(node, prev, &acct->work_list) {
  970. work = container_of(node, struct io_wq_work, list);
  971. if (!match->fn(work, match->data))
  972. continue;
  973. io_wq_remove_pending(wq, acct, work, prev);
  974. raw_spin_unlock(&acct->lock);
  975. io_run_cancel(work, wq);
  976. match->nr_pending++;
  977. /* not safe to continue after unlock */
  978. return true;
  979. }
  980. raw_spin_unlock(&acct->lock);
  981. return false;
  982. }
  983. static void io_wq_cancel_pending_work(struct io_wq *wq,
  984. struct io_cb_cancel_data *match)
  985. {
  986. int i;
  987. retry:
  988. for (i = 0; i < IO_WQ_ACCT_NR; i++) {
  989. struct io_wq_acct *acct = io_get_acct(wq, i == 0);
  990. if (io_acct_cancel_pending_work(wq, acct, match)) {
  991. if (match->cancel_all)
  992. goto retry;
  993. break;
  994. }
  995. }
  996. }
  997. static void io_acct_cancel_running_work(struct io_wq_acct *acct,
  998. struct io_cb_cancel_data *match)
  999. {
  1000. raw_spin_lock(&acct->workers_lock);
  1001. io_acct_for_each_worker(acct, io_wq_worker_cancel, match);
  1002. raw_spin_unlock(&acct->workers_lock);
  1003. }
  1004. static void io_wq_cancel_running_work(struct io_wq *wq,
  1005. struct io_cb_cancel_data *match)
  1006. {
  1007. rcu_read_lock();
  1008. for (int i = 0; i < IO_WQ_ACCT_NR; i++)
  1009. io_acct_cancel_running_work(&wq->acct[i], match);
  1010. rcu_read_unlock();
  1011. }
  1012. enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
  1013. void *data, bool cancel_all)
  1014. {
  1015. struct io_cb_cancel_data match = {
  1016. .fn = cancel,
  1017. .data = data,
  1018. .cancel_all = cancel_all,
  1019. };
  1020. /*
  1021. * First check pending list, if we're lucky we can just remove it
  1022. * from there. CANCEL_OK means that the work is returned as-new,
  1023. * no completion will be posted for it.
  1024. *
  1025. * Then check if a free (going busy) or busy worker has the work
  1026. * currently running. If we find it there, we'll return CANCEL_RUNNING
  1027. * as an indication that we attempt to signal cancellation. The
  1028. * completion will run normally in this case.
  1029. *
  1030. * Do both of these while holding the acct->workers_lock, to ensure that
  1031. * we'll find a work item regardless of state.
  1032. */
  1033. io_wq_cancel_pending_work(wq, &match);
  1034. if (match.nr_pending && !match.cancel_all)
  1035. return IO_WQ_CANCEL_OK;
  1036. io_wq_cancel_running_work(wq, &match);
  1037. if (match.nr_running && !match.cancel_all)
  1038. return IO_WQ_CANCEL_RUNNING;
  1039. if (match.nr_running)
  1040. return IO_WQ_CANCEL_RUNNING;
  1041. if (match.nr_pending)
  1042. return IO_WQ_CANCEL_OK;
  1043. return IO_WQ_CANCEL_NOTFOUND;
  1044. }
  1045. static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode,
  1046. int sync, void *key)
  1047. {
  1048. struct io_wq *wq = container_of(wait, struct io_wq, wait);
  1049. int i;
  1050. list_del_init(&wait->entry);
  1051. rcu_read_lock();
  1052. for (i = 0; i < IO_WQ_ACCT_NR; i++) {
  1053. struct io_wq_acct *acct = &wq->acct[i];
  1054. if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
  1055. io_acct_activate_free_worker(acct);
  1056. }
  1057. rcu_read_unlock();
  1058. return 1;
  1059. }
  1060. struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
  1061. {
  1062. int ret, i;
  1063. struct io_wq *wq;
  1064. if (WARN_ON_ONCE(!bounded))
  1065. return ERR_PTR(-EINVAL);
  1066. wq = kzalloc_obj(struct io_wq);
  1067. if (!wq)
  1068. return ERR_PTR(-ENOMEM);
  1069. refcount_inc(&data->hash->refs);
  1070. wq->hash = data->hash;
  1071. ret = -ENOMEM;
  1072. if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL))
  1073. goto err;
  1074. cpuset_cpus_allowed(data->task, wq->cpu_mask);
  1075. wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
  1076. wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
  1077. task_rlimit(current, RLIMIT_NPROC);
  1078. INIT_LIST_HEAD(&wq->wait.entry);
  1079. wq->wait.func = io_wq_hash_wake;
  1080. for (i = 0; i < IO_WQ_ACCT_NR; i++) {
  1081. struct io_wq_acct *acct = &wq->acct[i];
  1082. atomic_set(&acct->nr_running, 0);
  1083. raw_spin_lock_init(&acct->workers_lock);
  1084. INIT_HLIST_NULLS_HEAD(&acct->free_list, 0);
  1085. INIT_LIST_HEAD(&acct->all_list);
  1086. INIT_WQ_LIST(&acct->work_list);
  1087. raw_spin_lock_init(&acct->lock);
  1088. }
  1089. wq->task = get_task_struct(data->task);
  1090. atomic_set(&wq->worker_refs, 1);
  1091. init_completion(&wq->worker_done);
  1092. ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
  1093. if (ret) {
  1094. put_task_struct(wq->task);
  1095. goto err;
  1096. }
  1097. return wq;
  1098. err:
  1099. io_wq_put_hash(data->hash);
  1100. free_cpumask_var(wq->cpu_mask);
  1101. kfree(wq);
  1102. return ERR_PTR(ret);
  1103. }
  1104. static bool io_task_work_match(struct callback_head *cb, void *data)
  1105. {
  1106. struct io_worker *worker;
  1107. if (cb->func != create_worker_cb && cb->func != create_worker_cont)
  1108. return false;
  1109. worker = container_of(cb, struct io_worker, create_work);
  1110. return worker->wq == data;
  1111. }
  1112. void io_wq_exit_start(struct io_wq *wq)
  1113. {
  1114. set_bit(IO_WQ_BIT_EXIT, &wq->state);
  1115. }
  1116. static void io_wq_cancel_tw_create(struct io_wq *wq)
  1117. {
  1118. struct callback_head *cb;
  1119. while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
  1120. struct io_worker *worker;
  1121. worker = container_of(cb, struct io_worker, create_work);
  1122. io_worker_cancel_cb(worker);
  1123. /*
  1124. * Only the worker continuation helper has worker allocated and
  1125. * hence needs freeing.
  1126. */
  1127. if (cb->func == create_worker_cont)
  1128. kfree(worker);
  1129. }
  1130. }
  1131. static void io_wq_exit_workers(struct io_wq *wq)
  1132. {
  1133. unsigned long timeout, warn_timeout;
  1134. if (!wq->task)
  1135. return;
  1136. io_wq_cancel_tw_create(wq);
  1137. rcu_read_lock();
  1138. io_wq_for_each_worker(wq, io_wq_worker_wake, NULL);
  1139. rcu_read_unlock();
  1140. io_worker_ref_put(wq);
  1141. /*
  1142. * Shut up hung task complaint, see for example
  1143. *
  1144. * https://lore.kernel.org/all/696fc9e7.a70a0220.111c58.0006.GAE@google.com/
  1145. *
  1146. * where completely overloading the system with tons of long running
  1147. * io-wq items can easily trigger the hung task timeout. Only sleep
  1148. * uninterruptibly for half that time, and warn if we exceeded end
  1149. * up waiting more than IO_URING_EXIT_WAIT_MAX.
  1150. */
  1151. timeout = sysctl_hung_task_timeout_secs * HZ / 2;
  1152. if (!timeout)
  1153. timeout = MAX_SCHEDULE_TIMEOUT;
  1154. warn_timeout = jiffies + IO_URING_EXIT_WAIT_MAX;
  1155. do {
  1156. if (wait_for_completion_timeout(&wq->worker_done, timeout))
  1157. break;
  1158. WARN_ON_ONCE(time_after(jiffies, warn_timeout));
  1159. } while (1);
  1160. spin_lock_irq(&wq->hash->wait.lock);
  1161. list_del_init(&wq->wait.entry);
  1162. spin_unlock_irq(&wq->hash->wait.lock);
  1163. put_task_struct(wq->task);
  1164. wq->task = NULL;
  1165. }
  1166. static void io_wq_destroy(struct io_wq *wq)
  1167. {
  1168. struct io_cb_cancel_data match = {
  1169. .fn = io_wq_work_match_all,
  1170. .cancel_all = true,
  1171. };
  1172. cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
  1173. io_wq_cancel_pending_work(wq, &match);
  1174. free_cpumask_var(wq->cpu_mask);
  1175. io_wq_put_hash(wq->hash);
  1176. kfree(wq);
  1177. }
  1178. void io_wq_put_and_exit(struct io_wq *wq)
  1179. {
  1180. WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
  1181. io_wq_exit_workers(wq);
  1182. io_wq_destroy(wq);
  1183. }
  1184. struct online_data {
  1185. unsigned int cpu;
  1186. bool online;
  1187. };
  1188. static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
  1189. {
  1190. struct online_data *od = data;
  1191. if (od->online)
  1192. cpumask_set_cpu(od->cpu, worker->wq->cpu_mask);
  1193. else
  1194. cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask);
  1195. return false;
  1196. }
  1197. static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
  1198. {
  1199. struct online_data od = {
  1200. .cpu = cpu,
  1201. .online = online
  1202. };
  1203. rcu_read_lock();
  1204. io_wq_for_each_worker(wq, io_wq_worker_affinity, &od);
  1205. rcu_read_unlock();
  1206. return 0;
  1207. }
  1208. static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
  1209. {
  1210. struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
  1211. return __io_wq_cpu_online(wq, cpu, true);
  1212. }
  1213. static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
  1214. {
  1215. struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
  1216. return __io_wq_cpu_online(wq, cpu, false);
  1217. }
  1218. int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask)
  1219. {
  1220. cpumask_var_t allowed_mask;
  1221. int ret = 0;
  1222. if (!tctx || !tctx->io_wq)
  1223. return -EINVAL;
  1224. if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL))
  1225. return -ENOMEM;
  1226. rcu_read_lock();
  1227. cpuset_cpus_allowed(tctx->io_wq->task, allowed_mask);
  1228. if (mask) {
  1229. if (cpumask_subset(mask, allowed_mask))
  1230. cpumask_copy(tctx->io_wq->cpu_mask, mask);
  1231. else
  1232. ret = -EINVAL;
  1233. } else {
  1234. cpumask_copy(tctx->io_wq->cpu_mask, allowed_mask);
  1235. }
  1236. rcu_read_unlock();
  1237. free_cpumask_var(allowed_mask);
  1238. return ret;
  1239. }
  1240. /*
  1241. * Set max number of unbounded workers, returns old value. If new_count is 0,
  1242. * then just return the old value.
  1243. */
  1244. int io_wq_max_workers(struct io_wq *wq, int *new_count)
  1245. {
  1246. struct io_wq_acct *acct;
  1247. int prev[IO_WQ_ACCT_NR];
  1248. int i;
  1249. BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND);
  1250. BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
  1251. BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2);
  1252. for (i = 0; i < IO_WQ_ACCT_NR; i++) {
  1253. if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
  1254. new_count[i] = task_rlimit(current, RLIMIT_NPROC);
  1255. }
  1256. for (i = 0; i < IO_WQ_ACCT_NR; i++)
  1257. prev[i] = 0;
  1258. rcu_read_lock();
  1259. for (i = 0; i < IO_WQ_ACCT_NR; i++) {
  1260. acct = &wq->acct[i];
  1261. raw_spin_lock(&acct->workers_lock);
  1262. prev[i] = max_t(int, acct->max_workers, prev[i]);
  1263. if (new_count[i])
  1264. acct->max_workers = new_count[i];
  1265. raw_spin_unlock(&acct->workers_lock);
  1266. }
  1267. rcu_read_unlock();
  1268. for (i = 0; i < IO_WQ_ACCT_NR; i++)
  1269. new_count[i] = prev[i];
  1270. return 0;
  1271. }
  1272. static __init int io_wq_init(void)
  1273. {
  1274. int ret;
  1275. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
  1276. io_wq_cpu_online, io_wq_cpu_offline);
  1277. if (ret < 0)
  1278. return ret;
  1279. io_wq_online = ret;
  1280. return 0;
  1281. }
  1282. subsys_initcall(io_wq_init);