async-thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. atomic_read(&worker->num_pending) == 0) {
  162. freeit = 1;
  163. list_del_init(&worker->worker_list);
  164. worker->workers->num_workers--;
  165. }
  166. spin_unlock(&worker->workers->lock);
  167. spin_unlock_irq(&worker->lock);
  168. if (freeit)
  169. put_worker(worker);
  170. return freeit;
  171. }
  172. static struct btrfs_work *get_next_work(struct btrfs_worker_thread *worker,
  173. struct list_head *prio_head,
  174. struct list_head *head)
  175. {
  176. struct btrfs_work *work = NULL;
  177. struct list_head *cur = NULL;
  178. if(!list_empty(prio_head))
  179. cur = prio_head->next;
  180. smp_mb();
  181. if (!list_empty(&worker->prio_pending))
  182. goto refill;
  183. if (!list_empty(head))
  184. cur = head->next;
  185. if (cur)
  186. goto out;
  187. refill:
  188. spin_lock_irq(&worker->lock);
  189. list_splice_tail_init(&worker->prio_pending, prio_head);
  190. list_splice_tail_init(&worker->pending, head);
  191. if (!list_empty(prio_head))
  192. cur = prio_head->next;
  193. else if (!list_empty(head))
  194. cur = head->next;
  195. spin_unlock_irq(&worker->lock);
  196. if (!cur)
  197. goto out_fail;
  198. out:
  199. work = list_entry(cur, struct btrfs_work, list);
  200. out_fail:
  201. return work;
  202. }
  203. /*
  204. * main loop for servicing work items
  205. */
  206. static int worker_loop(void *arg)
  207. {
  208. struct btrfs_worker_thread *worker = arg;
  209. struct list_head head;
  210. struct list_head prio_head;
  211. struct btrfs_work *work;
  212. INIT_LIST_HEAD(&head);
  213. INIT_LIST_HEAD(&prio_head);
  214. do {
  215. again:
  216. while (1) {
  217. work = get_next_work(worker, &prio_head, &head);
  218. if (!work)
  219. break;
  220. list_del(&work->list);
  221. clear_bit(WORK_QUEUED_BIT, &work->flags);
  222. work->worker = worker;
  223. work->func(work);
  224. atomic_dec(&worker->num_pending);
  225. /*
  226. * unless this is an ordered work queue,
  227. * 'work' was probably freed by func above.
  228. */
  229. run_ordered_completions(worker->workers, work);
  230. check_pending_worker_creates(worker);
  231. }
  232. spin_lock_irq(&worker->lock);
  233. check_idle_worker(worker);
  234. if (freezing(current)) {
  235. worker->working = 0;
  236. spin_unlock_irq(&worker->lock);
  237. refrigerator();
  238. } else {
  239. spin_unlock_irq(&worker->lock);
  240. if (!kthread_should_stop()) {
  241. cpu_relax();
  242. /*
  243. * we've dropped the lock, did someone else
  244. * jump_in?
  245. */
  246. smp_mb();
  247. if (!list_empty(&worker->pending) ||
  248. !list_empty(&worker->prio_pending))
  249. continue;
  250. /*
  251. * this short schedule allows more work to
  252. * come in without the queue functions
  253. * needing to go through wake_up_process()
  254. *
  255. * worker->working is still 1, so nobody
  256. * is going to try and wake us up
  257. */
  258. schedule_timeout(1);
  259. smp_mb();
  260. if (!list_empty(&worker->pending) ||
  261. !list_empty(&worker->prio_pending))
  262. continue;
  263. if (kthread_should_stop())
  264. break;
  265. /* still no more work?, sleep for real */
  266. spin_lock_irq(&worker->lock);
  267. set_current_state(TASK_INTERRUPTIBLE);
  268. if (!list_empty(&worker->pending) ||
  269. !list_empty(&worker->prio_pending)) {
  270. spin_unlock_irq(&worker->lock);
  271. goto again;
  272. }
  273. /*
  274. * this makes sure we get a wakeup when someone
  275. * adds something new to the queue
  276. */
  277. worker->working = 0;
  278. spin_unlock_irq(&worker->lock);
  279. if (!kthread_should_stop()) {
  280. schedule_timeout(HZ * 120);
  281. if (!worker->working &&
  282. try_worker_shutdown(worker)) {
  283. return 0;
  284. }
  285. }
  286. }
  287. __set_current_state(TASK_RUNNING);
  288. }
  289. } while (!kthread_should_stop());
  290. return 0;
  291. }
  292. /*
  293. * this will wait for all the worker threads to shutdown
  294. */
  295. int btrfs_stop_workers(struct btrfs_workers *workers)
  296. {
  297. struct list_head *cur;
  298. struct btrfs_worker_thread *worker;
  299. int can_stop;
  300. spin_lock_irq(&workers->lock);
  301. list_splice_init(&workers->idle_list, &workers->worker_list);
  302. while (!list_empty(&workers->worker_list)) {
  303. cur = workers->worker_list.next;
  304. worker = list_entry(cur, struct btrfs_worker_thread,
  305. worker_list);
  306. atomic_inc(&worker->refs);
  307. workers->num_workers -= 1;
  308. if (!list_empty(&worker->worker_list)) {
  309. list_del_init(&worker->worker_list);
  310. put_worker(worker);
  311. can_stop = 1;
  312. } else
  313. can_stop = 0;
  314. spin_unlock_irq(&workers->lock);
  315. if (can_stop)
  316. kthread_stop(worker->task);
  317. spin_lock_irq(&workers->lock);
  318. put_worker(worker);
  319. }
  320. spin_unlock_irq(&workers->lock);
  321. return 0;
  322. }
  323. /*
  324. * simple init on struct btrfs_workers
  325. */
  326. void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max)
  327. {
  328. workers->num_workers = 0;
  329. INIT_LIST_HEAD(&workers->worker_list);
  330. INIT_LIST_HEAD(&workers->idle_list);
  331. INIT_LIST_HEAD(&workers->order_list);
  332. INIT_LIST_HEAD(&workers->prio_order_list);
  333. spin_lock_init(&workers->lock);
  334. spin_lock_init(&workers->order_lock);
  335. workers->max_workers = max;
  336. workers->idle_thresh = 32;
  337. workers->name = name;
  338. workers->ordered = 0;
  339. workers->atomic_start_pending = 0;
  340. workers->atomic_worker_start = 0;
  341. }
  342. /*
  343. * starts new worker threads. This does not enforce the max worker
  344. * count in case you need to temporarily go past it.
  345. */
  346. int btrfs_start_workers(struct btrfs_workers *workers, int num_workers)
  347. {
  348. struct btrfs_worker_thread *worker;
  349. int ret = 0;
  350. int i;
  351. for (i = 0; i < num_workers; i++) {
  352. worker = kzalloc(sizeof(*worker), GFP_NOFS);
  353. if (!worker) {
  354. ret = -ENOMEM;
  355. goto fail;
  356. }
  357. INIT_LIST_HEAD(&worker->pending);
  358. INIT_LIST_HEAD(&worker->prio_pending);
  359. INIT_LIST_HEAD(&worker->worker_list);
  360. spin_lock_init(&worker->lock);
  361. atomic_set(&worker->num_pending, 0);
  362. atomic_set(&worker->refs, 1);
  363. worker->workers = workers;
  364. worker->task = kthread_run(worker_loop, worker,
  365. "btrfs-%s-%d", workers->name,
  366. workers->num_workers + i);
  367. if (IS_ERR(worker->task)) {
  368. ret = PTR_ERR(worker->task);
  369. kfree(worker);
  370. goto fail;
  371. }
  372. spin_lock_irq(&workers->lock);
  373. list_add_tail(&worker->worker_list, &workers->idle_list);
  374. worker->idle = 1;
  375. workers->num_workers++;
  376. spin_unlock_irq(&workers->lock);
  377. }
  378. return 0;
  379. fail:
  380. btrfs_stop_workers(workers);
  381. return ret;
  382. }
  383. /*
  384. * run through the list and find a worker thread that doesn't have a lot
  385. * to do right now. This can return null if we aren't yet at the thread
  386. * count limit and all of the threads are busy.
  387. */
  388. static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
  389. {
  390. struct btrfs_worker_thread *worker;
  391. struct list_head *next;
  392. int enforce_min = workers->num_workers < workers->max_workers;
  393. /*
  394. * if we find an idle thread, don't move it to the end of the
  395. * idle list. This improves the chance that the next submission
  396. * will reuse the same thread, and maybe catch it while it is still
  397. * working
  398. */
  399. if (!list_empty(&workers->idle_list)) {
  400. next = workers->idle_list.next;
  401. worker = list_entry(next, struct btrfs_worker_thread,
  402. worker_list);
  403. return worker;
  404. }
  405. if (enforce_min || list_empty(&workers->worker_list))
  406. return NULL;
  407. /*
  408. * if we pick a busy task, move the task to the end of the list.
  409. * hopefully this will keep things somewhat evenly balanced.
  410. * Do the move in batches based on the sequence number. This groups
  411. * requests submitted at roughly the same time onto the same worker.
  412. */
  413. next = workers->worker_list.next;
  414. worker = list_entry(next, struct btrfs_worker_thread, worker_list);
  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. goto found;
  447. fallback:
  448. fallback = NULL;
  449. /*
  450. * we have failed to find any workers, just
  451. * return the first one we can find.
  452. */
  453. if (!list_empty(&workers->worker_list))
  454. fallback = workers->worker_list.next;
  455. if (!list_empty(&workers->idle_list))
  456. fallback = workers->idle_list.next;
  457. BUG_ON(!fallback);
  458. worker = list_entry(fallback,
  459. struct btrfs_worker_thread, worker_list);
  460. found:
  461. /*
  462. * this makes sure the worker doesn't exit before it is placed
  463. * onto a busy/idle list
  464. */
  465. atomic_inc(&worker->num_pending);
  466. spin_unlock_irqrestore(&workers->lock, flags);
  467. return worker;
  468. }
  469. /*
  470. * btrfs_requeue_work just puts the work item back on the tail of the list
  471. * it was taken from. It is intended for use with long running work functions
  472. * that make some progress and want to give the cpu up for others.
  473. */
  474. int btrfs_requeue_work(struct btrfs_work *work)
  475. {
  476. struct btrfs_worker_thread *worker = work->worker;
  477. unsigned long flags;
  478. int wake = 0;
  479. if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
  480. goto out;
  481. spin_lock_irqsave(&worker->lock, flags);
  482. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags))
  483. list_add_tail(&work->list, &worker->prio_pending);
  484. else
  485. list_add_tail(&work->list, &worker->pending);
  486. atomic_inc(&worker->num_pending);
  487. /* by definition we're busy, take ourselves off the idle
  488. * list
  489. */
  490. if (worker->idle) {
  491. spin_lock(&worker->workers->lock);
  492. worker->idle = 0;
  493. list_move_tail(&worker->worker_list,
  494. &worker->workers->worker_list);
  495. spin_unlock(&worker->workers->lock);
  496. }
  497. if (!worker->working) {
  498. wake = 1;
  499. worker->working = 1;
  500. }
  501. if (wake)
  502. wake_up_process(worker->task);
  503. spin_unlock_irqrestore(&worker->lock, flags);
  504. out:
  505. return 0;
  506. }
  507. void btrfs_set_work_high_prio(struct btrfs_work *work)
  508. {
  509. set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
  510. }
  511. /*
  512. * places a struct btrfs_work into the pending queue of one of the kthreads
  513. */
  514. int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
  515. {
  516. struct btrfs_worker_thread *worker;
  517. unsigned long flags;
  518. int wake = 0;
  519. /* don't requeue something already on a list */
  520. if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
  521. goto out;
  522. worker = find_worker(workers);
  523. if (workers->ordered) {
  524. /*
  525. * you're not allowed to do ordered queues from an
  526. * interrupt handler
  527. */
  528. spin_lock(&workers->order_lock);
  529. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags)) {
  530. list_add_tail(&work->order_list,
  531. &workers->prio_order_list);
  532. } else {
  533. list_add_tail(&work->order_list, &workers->order_list);
  534. }
  535. spin_unlock(&workers->order_lock);
  536. } else {
  537. INIT_LIST_HEAD(&work->order_list);
  538. }
  539. spin_lock_irqsave(&worker->lock, flags);
  540. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags))
  541. list_add_tail(&work->list, &worker->prio_pending);
  542. else
  543. list_add_tail(&work->list, &worker->pending);
  544. check_busy_worker(worker);
  545. /*
  546. * avoid calling into wake_up_process if this thread has already
  547. * been kicked
  548. */
  549. if (!worker->working)
  550. wake = 1;
  551. worker->working = 1;
  552. if (wake)
  553. wake_up_process(worker->task);
  554. spin_unlock_irqrestore(&worker->lock, flags);
  555. out:
  556. return 0;
  557. }