async-thread.c 16 KB

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