async-thread.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kthread.h>
  19. #include <linux/slab.h>
  20. #include <linux/list.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/freezer.h>
  23. #include "async-thread.h"
  24. #define WORK_QUEUED_BIT 0
  25. #define WORK_DONE_BIT 1
  26. #define WORK_ORDER_DONE_BIT 2
  27. #define WORK_HIGH_PRIO_BIT 3
  28. /*
  29. * container for the kthread task pointer and the list of pending work
  30. * One of these is allocated per thread.
  31. */
  32. struct btrfs_worker_thread {
  33. /* pool we belong to */
  34. struct btrfs_workers *workers;
  35. /* list of struct btrfs_work that are waiting for service */
  36. struct list_head pending;
  37. struct list_head prio_pending;
  38. /* list of worker threads from struct btrfs_workers */
  39. struct list_head worker_list;
  40. /* kthread */
  41. struct task_struct *task;
  42. /* number of things on the pending list */
  43. atomic_t num_pending;
  44. /* reference counter for this struct */
  45. atomic_t refs;
  46. unsigned long sequence;
  47. /* protects the pending list. */
  48. spinlock_t lock;
  49. /* set to non-zero when this thread is already awake and kicking */
  50. int working;
  51. /* are we currently idle */
  52. int idle;
  53. };
  54. static int __btrfs_start_workers(struct btrfs_workers *workers);
  55. /*
  56. * btrfs_start_workers uses kthread_run, which can block waiting for memory
  57. * for a very long time. It will actually throttle on page writeback,
  58. * and so it may not make progress until after our btrfs worker threads
  59. * process all of the pending work structs in their queue
  60. *
  61. * This means we can't use btrfs_start_workers from inside a btrfs worker
  62. * thread that is used as part of cleaning dirty memory, which pretty much
  63. * involves all of the worker threads.
  64. *
  65. * Instead we have a helper queue who never has more than one thread
  66. * where we scheduler thread start operations. This worker_start struct
  67. * is used to contain the work and hold a pointer to the queue that needs
  68. * another worker.
  69. */
  70. struct worker_start {
  71. struct btrfs_work work;
  72. struct btrfs_workers *queue;
  73. };
  74. static void start_new_worker_func(struct btrfs_work *work)
  75. {
  76. struct worker_start *start;
  77. start = container_of(work, struct worker_start, work);
  78. __btrfs_start_workers(start->queue);
  79. kfree(start);
  80. }
  81. /*
  82. * helper function to move a thread onto the idle list after it
  83. * has finished some requests.
  84. */
  85. static void check_idle_worker(struct btrfs_worker_thread *worker)
  86. {
  87. if (!worker->idle && atomic_read(&worker->num_pending) <
  88. worker->workers->idle_thresh / 2) {
  89. unsigned long flags;
  90. spin_lock_irqsave(&worker->workers->lock, flags);
  91. worker->idle = 1;
  92. /* the list may be empty if the worker is just starting */
  93. if (!list_empty(&worker->worker_list)) {
  94. list_move(&worker->worker_list,
  95. &worker->workers->idle_list);
  96. }
  97. spin_unlock_irqrestore(&worker->workers->lock, flags);
  98. }
  99. }
  100. /*
  101. * helper function to move a thread off the idle list after new
  102. * pending work is added.
  103. */
  104. static void check_busy_worker(struct btrfs_worker_thread *worker)
  105. {
  106. if (worker->idle && atomic_read(&worker->num_pending) >=
  107. worker->workers->idle_thresh) {
  108. unsigned long flags;
  109. spin_lock_irqsave(&worker->workers->lock, flags);
  110. worker->idle = 0;
  111. if (!list_empty(&worker->worker_list)) {
  112. list_move_tail(&worker->worker_list,
  113. &worker->workers->worker_list);
  114. }
  115. spin_unlock_irqrestore(&worker->workers->lock, flags);
  116. }
  117. }
  118. static void check_pending_worker_creates(struct btrfs_worker_thread *worker)
  119. {
  120. struct btrfs_workers *workers = worker->workers;
  121. struct worker_start *start;
  122. unsigned long flags;
  123. rmb();
  124. if (!workers->atomic_start_pending)
  125. return;
  126. start = kzalloc(sizeof(*start), GFP_NOFS);
  127. if (!start)
  128. return;
  129. start->work.func = start_new_worker_func;
  130. start->queue = workers;
  131. spin_lock_irqsave(&workers->lock, flags);
  132. if (!workers->atomic_start_pending)
  133. goto out;
  134. workers->atomic_start_pending = 0;
  135. if (workers->num_workers + workers->num_workers_starting >=
  136. workers->max_workers)
  137. goto out;
  138. workers->num_workers_starting += 1;
  139. spin_unlock_irqrestore(&workers->lock, flags);
  140. btrfs_queue_worker(workers->atomic_worker_start, &start->work);
  141. return;
  142. out:
  143. kfree(start);
  144. spin_unlock_irqrestore(&workers->lock, flags);
  145. }
  146. static noinline void run_ordered_completions(struct btrfs_workers *workers,
  147. struct btrfs_work *work)
  148. {
  149. if (!workers->ordered)
  150. return;
  151. set_bit(WORK_DONE_BIT, &work->flags);
  152. spin_lock(&workers->order_lock);
  153. while (1) {
  154. if (!list_empty(&workers->prio_order_list)) {
  155. work = list_entry(workers->prio_order_list.next,
  156. struct btrfs_work, order_list);
  157. } else if (!list_empty(&workers->order_list)) {
  158. work = list_entry(workers->order_list.next,
  159. struct btrfs_work, order_list);
  160. } else {
  161. break;
  162. }
  163. if (!test_bit(WORK_DONE_BIT, &work->flags))
  164. break;
  165. /* we are going to call the ordered done function, but
  166. * we leave the work item on the list as a barrier so
  167. * that later work items that are done don't have their
  168. * functions called before this one returns
  169. */
  170. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  171. break;
  172. spin_unlock(&workers->order_lock);
  173. work->ordered_func(work);
  174. /* now take the lock again and call the freeing code */
  175. spin_lock(&workers->order_lock);
  176. list_del(&work->order_list);
  177. work->ordered_free(work);
  178. }
  179. spin_unlock(&workers->order_lock);
  180. }
  181. static void put_worker(struct btrfs_worker_thread *worker)
  182. {
  183. if (atomic_dec_and_test(&worker->refs))
  184. kfree(worker);
  185. }
  186. static int try_worker_shutdown(struct btrfs_worker_thread *worker)
  187. {
  188. int freeit = 0;
  189. spin_lock_irq(&worker->lock);
  190. spin_lock(&worker->workers->lock);
  191. if (worker->workers->num_workers > 1 &&
  192. worker->idle &&
  193. !worker->working &&
  194. !list_empty(&worker->worker_list) &&
  195. list_empty(&worker->prio_pending) &&
  196. list_empty(&worker->pending) &&
  197. atomic_read(&worker->num_pending) == 0) {
  198. freeit = 1;
  199. list_del_init(&worker->worker_list);
  200. worker->workers->num_workers--;
  201. }
  202. spin_unlock(&worker->workers->lock);
  203. spin_unlock_irq(&worker->lock);
  204. if (freeit)
  205. put_worker(worker);
  206. return freeit;
  207. }
  208. static struct btrfs_work *get_next_work(struct btrfs_worker_thread *worker,
  209. struct list_head *prio_head,
  210. struct list_head *head)
  211. {
  212. struct btrfs_work *work = NULL;
  213. struct list_head *cur = NULL;
  214. if(!list_empty(prio_head))
  215. cur = prio_head->next;
  216. smp_mb();
  217. if (!list_empty(&worker->prio_pending))
  218. goto refill;
  219. if (!list_empty(head))
  220. cur = head->next;
  221. if (cur)
  222. goto out;
  223. refill:
  224. spin_lock_irq(&worker->lock);
  225. list_splice_tail_init(&worker->prio_pending, prio_head);
  226. list_splice_tail_init(&worker->pending, head);
  227. if (!list_empty(prio_head))
  228. cur = prio_head->next;
  229. else if (!list_empty(head))
  230. cur = head->next;
  231. spin_unlock_irq(&worker->lock);
  232. if (!cur)
  233. goto out_fail;
  234. out:
  235. work = list_entry(cur, struct btrfs_work, list);
  236. out_fail:
  237. return work;
  238. }
  239. /*
  240. * main loop for servicing work items
  241. */
  242. static int worker_loop(void *arg)
  243. {
  244. struct btrfs_worker_thread *worker = arg;
  245. struct list_head head;
  246. struct list_head prio_head;
  247. struct btrfs_work *work;
  248. INIT_LIST_HEAD(&head);
  249. INIT_LIST_HEAD(&prio_head);
  250. do {
  251. again:
  252. while (1) {
  253. work = get_next_work(worker, &prio_head, &head);
  254. if (!work)
  255. break;
  256. list_del(&work->list);
  257. clear_bit(WORK_QUEUED_BIT, &work->flags);
  258. work->worker = worker;
  259. work->func(work);
  260. atomic_dec(&worker->num_pending);
  261. /*
  262. * unless this is an ordered work queue,
  263. * 'work' was probably freed by func above.
  264. */
  265. run_ordered_completions(worker->workers, work);
  266. check_pending_worker_creates(worker);
  267. cond_resched();
  268. }
  269. spin_lock_irq(&worker->lock);
  270. check_idle_worker(worker);
  271. if (freezing(current)) {
  272. worker->working = 0;
  273. spin_unlock_irq(&worker->lock);
  274. try_to_freeze();
  275. } else {
  276. spin_unlock_irq(&worker->lock);
  277. if (!kthread_should_stop()) {
  278. cpu_relax();
  279. /*
  280. * we've dropped the lock, did someone else
  281. * jump_in?
  282. */
  283. smp_mb();
  284. if (!list_empty(&worker->pending) ||
  285. !list_empty(&worker->prio_pending))
  286. continue;
  287. /*
  288. * this short schedule allows more work to
  289. * come in without the queue functions
  290. * needing to go through wake_up_process()
  291. *
  292. * worker->working is still 1, so nobody
  293. * is going to try and wake us up
  294. */
  295. schedule_timeout(1);
  296. smp_mb();
  297. if (!list_empty(&worker->pending) ||
  298. !list_empty(&worker->prio_pending))
  299. continue;
  300. if (kthread_should_stop())
  301. break;
  302. /* still no more work?, sleep for real */
  303. spin_lock_irq(&worker->lock);
  304. set_current_state(TASK_INTERRUPTIBLE);
  305. if (!list_empty(&worker->pending) ||
  306. !list_empty(&worker->prio_pending)) {
  307. spin_unlock_irq(&worker->lock);
  308. set_current_state(TASK_RUNNING);
  309. goto again;
  310. }
  311. /*
  312. * this makes sure we get a wakeup when someone
  313. * adds something new to the queue
  314. */
  315. worker->working = 0;
  316. spin_unlock_irq(&worker->lock);
  317. if (!kthread_should_stop()) {
  318. schedule_timeout(HZ * 120);
  319. if (!worker->working &&
  320. try_worker_shutdown(worker)) {
  321. return 0;
  322. }
  323. }
  324. }
  325. __set_current_state(TASK_RUNNING);
  326. }
  327. } while (!kthread_should_stop());
  328. return 0;
  329. }
  330. /*
  331. * this will wait for all the worker threads to shutdown
  332. */
  333. void btrfs_stop_workers(struct btrfs_workers *workers)
  334. {
  335. struct list_head *cur;
  336. struct btrfs_worker_thread *worker;
  337. int can_stop;
  338. spin_lock_irq(&workers->lock);
  339. list_splice_init(&workers->idle_list, &workers->worker_list);
  340. while (!list_empty(&workers->worker_list)) {
  341. cur = workers->worker_list.next;
  342. worker = list_entry(cur, struct btrfs_worker_thread,
  343. worker_list);
  344. atomic_inc(&worker->refs);
  345. workers->num_workers -= 1;
  346. if (!list_empty(&worker->worker_list)) {
  347. list_del_init(&worker->worker_list);
  348. put_worker(worker);
  349. can_stop = 1;
  350. } else
  351. can_stop = 0;
  352. spin_unlock_irq(&workers->lock);
  353. if (can_stop)
  354. kthread_stop(worker->task);
  355. spin_lock_irq(&workers->lock);
  356. put_worker(worker);
  357. }
  358. spin_unlock_irq(&workers->lock);
  359. }
  360. /*
  361. * simple init on struct btrfs_workers
  362. */
  363. void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max,
  364. struct btrfs_workers *async_helper)
  365. {
  366. workers->num_workers = 0;
  367. workers->num_workers_starting = 0;
  368. INIT_LIST_HEAD(&workers->worker_list);
  369. INIT_LIST_HEAD(&workers->idle_list);
  370. INIT_LIST_HEAD(&workers->order_list);
  371. INIT_LIST_HEAD(&workers->prio_order_list);
  372. spin_lock_init(&workers->lock);
  373. spin_lock_init(&workers->order_lock);
  374. workers->max_workers = max;
  375. workers->idle_thresh = 32;
  376. workers->name = name;
  377. workers->ordered = 0;
  378. workers->atomic_start_pending = 0;
  379. workers->atomic_worker_start = async_helper;
  380. }
  381. /*
  382. * starts new worker threads. This does not enforce the max worker
  383. * count in case you need to temporarily go past it.
  384. */
  385. static int __btrfs_start_workers(struct btrfs_workers *workers)
  386. {
  387. struct btrfs_worker_thread *worker;
  388. int ret = 0;
  389. worker = kzalloc(sizeof(*worker), GFP_NOFS);
  390. if (!worker) {
  391. ret = -ENOMEM;
  392. goto fail;
  393. }
  394. INIT_LIST_HEAD(&worker->pending);
  395. INIT_LIST_HEAD(&worker->prio_pending);
  396. INIT_LIST_HEAD(&worker->worker_list);
  397. spin_lock_init(&worker->lock);
  398. atomic_set(&worker->num_pending, 0);
  399. atomic_set(&worker->refs, 1);
  400. worker->workers = workers;
  401. worker->task = kthread_run(worker_loop, worker,
  402. "btrfs-%s-%d", workers->name,
  403. workers->num_workers + 1);
  404. if (IS_ERR(worker->task)) {
  405. ret = PTR_ERR(worker->task);
  406. kfree(worker);
  407. goto fail;
  408. }
  409. spin_lock_irq(&workers->lock);
  410. list_add_tail(&worker->worker_list, &workers->idle_list);
  411. worker->idle = 1;
  412. workers->num_workers++;
  413. workers->num_workers_starting--;
  414. WARN_ON(workers->num_workers_starting < 0);
  415. spin_unlock_irq(&workers->lock);
  416. return 0;
  417. fail:
  418. spin_lock_irq(&workers->lock);
  419. workers->num_workers_starting--;
  420. spin_unlock_irq(&workers->lock);
  421. return ret;
  422. }
  423. int btrfs_start_workers(struct btrfs_workers *workers)
  424. {
  425. spin_lock_irq(&workers->lock);
  426. workers->num_workers_starting++;
  427. spin_unlock_irq(&workers->lock);
  428. return __btrfs_start_workers(workers);
  429. }
  430. /*
  431. * run through the list and find a worker thread that doesn't have a lot
  432. * to do right now. This can return null if we aren't yet at the thread
  433. * count limit and all of the threads are busy.
  434. */
  435. static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
  436. {
  437. struct btrfs_worker_thread *worker;
  438. struct list_head *next;
  439. int enforce_min;
  440. enforce_min = (workers->num_workers + workers->num_workers_starting) <
  441. workers->max_workers;
  442. /*
  443. * if we find an idle thread, don't move it to the end of the
  444. * idle list. This improves the chance that the next submission
  445. * will reuse the same thread, and maybe catch it while it is still
  446. * working
  447. */
  448. if (!list_empty(&workers->idle_list)) {
  449. next = workers->idle_list.next;
  450. worker = list_entry(next, struct btrfs_worker_thread,
  451. worker_list);
  452. return worker;
  453. }
  454. if (enforce_min || list_empty(&workers->worker_list))
  455. return NULL;
  456. /*
  457. * if we pick a busy task, move the task to the end of the list.
  458. * hopefully this will keep things somewhat evenly balanced.
  459. * Do the move in batches based on the sequence number. This groups
  460. * requests submitted at roughly the same time onto the same worker.
  461. */
  462. next = workers->worker_list.next;
  463. worker = list_entry(next, struct btrfs_worker_thread, worker_list);
  464. worker->sequence++;
  465. if (worker->sequence % workers->idle_thresh == 0)
  466. list_move_tail(next, &workers->worker_list);
  467. return worker;
  468. }
  469. /*
  470. * selects a worker thread to take the next job. This will either find
  471. * an idle worker, start a new worker up to the max count, or just return
  472. * one of the existing busy workers.
  473. */
  474. static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
  475. {
  476. struct btrfs_worker_thread *worker;
  477. unsigned long flags;
  478. struct list_head *fallback;
  479. int ret;
  480. spin_lock_irqsave(&workers->lock, flags);
  481. again:
  482. worker = next_worker(workers);
  483. if (!worker) {
  484. if (workers->num_workers + workers->num_workers_starting >=
  485. workers->max_workers) {
  486. goto fallback;
  487. } else if (workers->atomic_worker_start) {
  488. workers->atomic_start_pending = 1;
  489. goto fallback;
  490. } else {
  491. workers->num_workers_starting++;
  492. spin_unlock_irqrestore(&workers->lock, flags);
  493. /* we're below the limit, start another worker */
  494. ret = __btrfs_start_workers(workers);
  495. spin_lock_irqsave(&workers->lock, flags);
  496. if (ret)
  497. goto fallback;
  498. goto again;
  499. }
  500. }
  501. goto found;
  502. fallback:
  503. fallback = NULL;
  504. /*
  505. * we have failed to find any workers, just
  506. * return the first one we can find.
  507. */
  508. if (!list_empty(&workers->worker_list))
  509. fallback = workers->worker_list.next;
  510. if (!list_empty(&workers->idle_list))
  511. fallback = workers->idle_list.next;
  512. BUG_ON(!fallback);
  513. worker = list_entry(fallback,
  514. struct btrfs_worker_thread, worker_list);
  515. found:
  516. /*
  517. * this makes sure the worker doesn't exit before it is placed
  518. * onto a busy/idle list
  519. */
  520. atomic_inc(&worker->num_pending);
  521. spin_unlock_irqrestore(&workers->lock, flags);
  522. return worker;
  523. }
  524. /*
  525. * btrfs_requeue_work just puts the work item back on the tail of the list
  526. * it was taken from. It is intended for use with long running work functions
  527. * that make some progress and want to give the cpu up for others.
  528. */
  529. void btrfs_requeue_work(struct btrfs_work *work)
  530. {
  531. struct btrfs_worker_thread *worker = work->worker;
  532. unsigned long flags;
  533. int wake = 0;
  534. if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
  535. return;
  536. spin_lock_irqsave(&worker->lock, flags);
  537. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags))
  538. list_add_tail(&work->list, &worker->prio_pending);
  539. else
  540. list_add_tail(&work->list, &worker->pending);
  541. atomic_inc(&worker->num_pending);
  542. /* by definition we're busy, take ourselves off the idle
  543. * list
  544. */
  545. if (worker->idle) {
  546. spin_lock(&worker->workers->lock);
  547. worker->idle = 0;
  548. list_move_tail(&worker->worker_list,
  549. &worker->workers->worker_list);
  550. spin_unlock(&worker->workers->lock);
  551. }
  552. if (!worker->working) {
  553. wake = 1;
  554. worker->working = 1;
  555. }
  556. if (wake)
  557. wake_up_process(worker->task);
  558. spin_unlock_irqrestore(&worker->lock, flags);
  559. }
  560. void btrfs_set_work_high_prio(struct btrfs_work *work)
  561. {
  562. set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
  563. }
  564. /*
  565. * places a struct btrfs_work into the pending queue of one of the kthreads
  566. */
  567. void btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
  568. {
  569. struct btrfs_worker_thread *worker;
  570. unsigned long flags;
  571. int wake = 0;
  572. /* don't requeue something already on a list */
  573. if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
  574. return;
  575. worker = find_worker(workers);
  576. if (workers->ordered) {
  577. /*
  578. * you're not allowed to do ordered queues from an
  579. * interrupt handler
  580. */
  581. spin_lock(&workers->order_lock);
  582. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags)) {
  583. list_add_tail(&work->order_list,
  584. &workers->prio_order_list);
  585. } else {
  586. list_add_tail(&work->order_list, &workers->order_list);
  587. }
  588. spin_unlock(&workers->order_lock);
  589. } else {
  590. INIT_LIST_HEAD(&work->order_list);
  591. }
  592. spin_lock_irqsave(&worker->lock, flags);
  593. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags))
  594. list_add_tail(&work->list, &worker->prio_pending);
  595. else
  596. list_add_tail(&work->list, &worker->pending);
  597. check_busy_worker(worker);
  598. /*
  599. * avoid calling into wake_up_process if this thread has already
  600. * been kicked
  601. */
  602. if (!worker->working)
  603. wake = 1;
  604. worker->working = 1;
  605. if (wake)
  606. wake_up_process(worker->task);
  607. spin_unlock_irqrestore(&worker->lock, flags);
  608. }