async-thread.c 16 KB

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