async-thread.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/version.h>
  19. #include <linux/kthread.h>
  20. #include <linux/list.h>
  21. #include <linux/spinlock.h>
  22. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  23. # include <linux/freezer.h>
  24. #else
  25. # include <linux/sched.h>
  26. #endif
  27. #include "async-thread.h"
  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. /* 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. /* protects the pending list. */
  44. spinlock_t lock;
  45. /* set to non-zero when this thread is already awake and kicking */
  46. int working;
  47. /* are we currently idle */
  48. int idle;
  49. };
  50. /*
  51. * helper function to move a thread onto the idle list after it
  52. * has finished some requests.
  53. */
  54. static void check_idle_worker(struct btrfs_worker_thread *worker)
  55. {
  56. if (!worker->idle && atomic_read(&worker->num_pending) <
  57. worker->workers->idle_thresh / 2) {
  58. unsigned long flags;
  59. spin_lock_irqsave(&worker->workers->lock, flags);
  60. worker->idle = 1;
  61. list_move(&worker->worker_list, &worker->workers->idle_list);
  62. spin_unlock_irqrestore(&worker->workers->lock, flags);
  63. }
  64. }
  65. /*
  66. * helper function to move a thread off the idle list after new
  67. * pending work is added.
  68. */
  69. static void check_busy_worker(struct btrfs_worker_thread *worker)
  70. {
  71. if (worker->idle && atomic_read(&worker->num_pending) >=
  72. worker->workers->idle_thresh) {
  73. unsigned long flags;
  74. spin_lock_irqsave(&worker->workers->lock, flags);
  75. worker->idle = 0;
  76. list_move_tail(&worker->worker_list,
  77. &worker->workers->worker_list);
  78. spin_unlock_irqrestore(&worker->workers->lock, flags);
  79. }
  80. }
  81. /*
  82. * main loop for servicing work items
  83. */
  84. static int worker_loop(void *arg)
  85. {
  86. struct btrfs_worker_thread *worker = arg;
  87. struct list_head *cur;
  88. struct btrfs_work *work;
  89. do {
  90. spin_lock_irq(&worker->lock);
  91. while(!list_empty(&worker->pending)) {
  92. cur = worker->pending.next;
  93. work = list_entry(cur, struct btrfs_work, list);
  94. list_del(&work->list);
  95. clear_bit(0, &work->flags);
  96. work->worker = worker;
  97. spin_unlock_irq(&worker->lock);
  98. work->func(work);
  99. atomic_dec(&worker->num_pending);
  100. spin_lock_irq(&worker->lock);
  101. check_idle_worker(worker);
  102. }
  103. worker->working = 0;
  104. if (freezing(current)) {
  105. refrigerator();
  106. } else {
  107. set_current_state(TASK_INTERRUPTIBLE);
  108. spin_unlock_irq(&worker->lock);
  109. schedule();
  110. __set_current_state(TASK_RUNNING);
  111. }
  112. } while (!kthread_should_stop());
  113. return 0;
  114. }
  115. /*
  116. * this will wait for all the worker threads to shutdown
  117. */
  118. int btrfs_stop_workers(struct btrfs_workers *workers)
  119. {
  120. struct list_head *cur;
  121. struct btrfs_worker_thread *worker;
  122. list_splice_init(&workers->idle_list, &workers->worker_list);
  123. while(!list_empty(&workers->worker_list)) {
  124. cur = workers->worker_list.next;
  125. worker = list_entry(cur, struct btrfs_worker_thread,
  126. worker_list);
  127. kthread_stop(worker->task);
  128. list_del(&worker->worker_list);
  129. kfree(worker);
  130. }
  131. return 0;
  132. }
  133. /*
  134. * simple init on struct btrfs_workers
  135. */
  136. void btrfs_init_workers(struct btrfs_workers *workers, int max)
  137. {
  138. workers->num_workers = 0;
  139. INIT_LIST_HEAD(&workers->worker_list);
  140. INIT_LIST_HEAD(&workers->idle_list);
  141. spin_lock_init(&workers->lock);
  142. workers->max_workers = max;
  143. workers->idle_thresh = 32;
  144. }
  145. /*
  146. * starts new worker threads. This does not enforce the max worker
  147. * count in case you need to temporarily go past it.
  148. */
  149. int btrfs_start_workers(struct btrfs_workers *workers, int num_workers)
  150. {
  151. struct btrfs_worker_thread *worker;
  152. int ret = 0;
  153. int i;
  154. for (i = 0; i < num_workers; i++) {
  155. worker = kzalloc(sizeof(*worker), GFP_NOFS);
  156. if (!worker) {
  157. ret = -ENOMEM;
  158. goto fail;
  159. }
  160. INIT_LIST_HEAD(&worker->pending);
  161. INIT_LIST_HEAD(&worker->worker_list);
  162. spin_lock_init(&worker->lock);
  163. atomic_set(&worker->num_pending, 0);
  164. worker->task = kthread_run(worker_loop, worker, "btrfs");
  165. worker->workers = workers;
  166. if (IS_ERR(worker->task)) {
  167. kfree(worker);
  168. ret = PTR_ERR(worker->task);
  169. goto fail;
  170. }
  171. spin_lock_irq(&workers->lock);
  172. list_add_tail(&worker->worker_list, &workers->idle_list);
  173. workers->num_workers++;
  174. spin_unlock_irq(&workers->lock);
  175. }
  176. return 0;
  177. fail:
  178. btrfs_stop_workers(workers);
  179. return ret;
  180. }
  181. /*
  182. * run through the list and find a worker thread that doesn't have a lot
  183. * to do right now. This can return null if we aren't yet at the thread
  184. * count limit and all of the threads are busy.
  185. */
  186. static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
  187. {
  188. struct btrfs_worker_thread *worker;
  189. struct list_head *next;
  190. int enforce_min = workers->num_workers < workers->max_workers;
  191. /*
  192. * if we find an idle thread, don't move it to the end of the
  193. * idle list. This improves the chance that the next submission
  194. * will reuse the same thread, and maybe catch it while it is still
  195. * working
  196. */
  197. if (!list_empty(&workers->idle_list)) {
  198. next = workers->idle_list.next;
  199. worker = list_entry(next, struct btrfs_worker_thread,
  200. worker_list);
  201. return worker;
  202. }
  203. if (enforce_min || list_empty(&workers->worker_list))
  204. return NULL;
  205. /*
  206. * if we pick a busy task, move the task to the end of the list.
  207. * hopefully this will keep things somewhat evenly balanced
  208. */
  209. next = workers->worker_list.next;
  210. worker = list_entry(next, struct btrfs_worker_thread, worker_list);
  211. list_move_tail(next, &workers->worker_list);
  212. return worker;
  213. }
  214. static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
  215. {
  216. struct btrfs_worker_thread *worker;
  217. unsigned long flags;
  218. again:
  219. spin_lock_irqsave(&workers->lock, flags);
  220. worker = next_worker(workers);
  221. spin_unlock_irqrestore(&workers->lock, flags);
  222. if (!worker) {
  223. spin_lock_irqsave(&workers->lock, flags);
  224. if (workers->num_workers >= workers->max_workers) {
  225. struct list_head *fallback = NULL;
  226. /*
  227. * we have failed to find any workers, just
  228. * return the force one
  229. */
  230. if (!list_empty(&workers->worker_list))
  231. fallback = workers->worker_list.next;
  232. if (!list_empty(&workers->idle_list))
  233. fallback = workers->idle_list.next;
  234. BUG_ON(!fallback);
  235. worker = list_entry(fallback,
  236. struct btrfs_worker_thread, worker_list);
  237. spin_unlock_irqrestore(&workers->lock, flags);
  238. } else {
  239. spin_unlock_irqrestore(&workers->lock, flags);
  240. /* we're below the limit, start another worker */
  241. btrfs_start_workers(workers, 1);
  242. goto again;
  243. }
  244. }
  245. return worker;
  246. }
  247. /*
  248. * btrfs_requeue_work just puts the work item back on the tail of the list
  249. * it was taken from. It is intended for use with long running work functions
  250. * that make some progress and want to give the cpu up for others.
  251. */
  252. int btrfs_requeue_work(struct btrfs_work *work)
  253. {
  254. struct btrfs_worker_thread *worker = work->worker;
  255. unsigned long flags;
  256. if (test_and_set_bit(0, &work->flags))
  257. goto out;
  258. spin_lock_irqsave(&worker->lock, flags);
  259. atomic_inc(&worker->num_pending);
  260. list_add_tail(&work->list, &worker->pending);
  261. check_busy_worker(worker);
  262. spin_unlock_irqrestore(&worker->lock, flags);
  263. out:
  264. return 0;
  265. }
  266. /*
  267. * places a struct btrfs_work into the pending queue of one of the kthreads
  268. */
  269. int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
  270. {
  271. struct btrfs_worker_thread *worker;
  272. unsigned long flags;
  273. int wake = 0;
  274. /* don't requeue something already on a list */
  275. if (test_and_set_bit(0, &work->flags))
  276. goto out;
  277. worker = find_worker(workers);
  278. spin_lock_irqsave(&worker->lock, flags);
  279. atomic_inc(&worker->num_pending);
  280. check_busy_worker(worker);
  281. list_add_tail(&work->list, &worker->pending);
  282. /*
  283. * avoid calling into wake_up_process if this thread has already
  284. * been kicked
  285. */
  286. if (!worker->working)
  287. wake = 1;
  288. worker->working = 1;
  289. spin_unlock_irqrestore(&worker->lock, flags);
  290. if (wake)
  291. wake_up_process(worker->task);
  292. out:
  293. return 0;
  294. }