async-thread.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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, char *name, 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. workers->name = name;
  145. }
  146. /*
  147. * starts new worker threads. This does not enforce the max worker
  148. * count in case you need to temporarily go past it.
  149. */
  150. int btrfs_start_workers(struct btrfs_workers *workers, int num_workers)
  151. {
  152. struct btrfs_worker_thread *worker;
  153. int ret = 0;
  154. int i;
  155. for (i = 0; i < num_workers; i++) {
  156. worker = kzalloc(sizeof(*worker), GFP_NOFS);
  157. if (!worker) {
  158. ret = -ENOMEM;
  159. goto fail;
  160. }
  161. INIT_LIST_HEAD(&worker->pending);
  162. INIT_LIST_HEAD(&worker->worker_list);
  163. spin_lock_init(&worker->lock);
  164. atomic_set(&worker->num_pending, 0);
  165. worker->task = kthread_run(worker_loop, worker,
  166. "btrfs-%s-%d", workers->name,
  167. workers->num_workers + i);
  168. worker->workers = workers;
  169. if (IS_ERR(worker->task)) {
  170. kfree(worker);
  171. ret = PTR_ERR(worker->task);
  172. goto fail;
  173. }
  174. spin_lock_irq(&workers->lock);
  175. list_add_tail(&worker->worker_list, &workers->idle_list);
  176. workers->num_workers++;
  177. spin_unlock_irq(&workers->lock);
  178. }
  179. return 0;
  180. fail:
  181. btrfs_stop_workers(workers);
  182. return ret;
  183. }
  184. /*
  185. * run through the list and find a worker thread that doesn't have a lot
  186. * to do right now. This can return null if we aren't yet at the thread
  187. * count limit and all of the threads are busy.
  188. */
  189. static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
  190. {
  191. struct btrfs_worker_thread *worker;
  192. struct list_head *next;
  193. int enforce_min = workers->num_workers < workers->max_workers;
  194. /*
  195. * if we find an idle thread, don't move it to the end of the
  196. * idle list. This improves the chance that the next submission
  197. * will reuse the same thread, and maybe catch it while it is still
  198. * working
  199. */
  200. if (!list_empty(&workers->idle_list)) {
  201. next = workers->idle_list.next;
  202. worker = list_entry(next, struct btrfs_worker_thread,
  203. worker_list);
  204. return worker;
  205. }
  206. if (enforce_min || list_empty(&workers->worker_list))
  207. return NULL;
  208. /*
  209. * if we pick a busy task, move the task to the end of the list.
  210. * hopefully this will keep things somewhat evenly balanced
  211. */
  212. next = workers->worker_list.next;
  213. worker = list_entry(next, struct btrfs_worker_thread, worker_list);
  214. list_move_tail(next, &workers->worker_list);
  215. return worker;
  216. }
  217. static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
  218. {
  219. struct btrfs_worker_thread *worker;
  220. unsigned long flags;
  221. again:
  222. spin_lock_irqsave(&workers->lock, flags);
  223. worker = next_worker(workers);
  224. spin_unlock_irqrestore(&workers->lock, flags);
  225. if (!worker) {
  226. spin_lock_irqsave(&workers->lock, flags);
  227. if (workers->num_workers >= workers->max_workers) {
  228. struct list_head *fallback = NULL;
  229. /*
  230. * we have failed to find any workers, just
  231. * return the force one
  232. */
  233. if (!list_empty(&workers->worker_list))
  234. fallback = workers->worker_list.next;
  235. if (!list_empty(&workers->idle_list))
  236. fallback = workers->idle_list.next;
  237. BUG_ON(!fallback);
  238. worker = list_entry(fallback,
  239. struct btrfs_worker_thread, worker_list);
  240. spin_unlock_irqrestore(&workers->lock, flags);
  241. } else {
  242. spin_unlock_irqrestore(&workers->lock, flags);
  243. /* we're below the limit, start another worker */
  244. btrfs_start_workers(workers, 1);
  245. goto again;
  246. }
  247. }
  248. return worker;
  249. }
  250. /*
  251. * btrfs_requeue_work just puts the work item back on the tail of the list
  252. * it was taken from. It is intended for use with long running work functions
  253. * that make some progress and want to give the cpu up for others.
  254. */
  255. int btrfs_requeue_work(struct btrfs_work *work)
  256. {
  257. struct btrfs_worker_thread *worker = work->worker;
  258. unsigned long flags;
  259. if (test_and_set_bit(0, &work->flags))
  260. goto out;
  261. spin_lock_irqsave(&worker->lock, flags);
  262. atomic_inc(&worker->num_pending);
  263. list_add_tail(&work->list, &worker->pending);
  264. check_busy_worker(worker);
  265. spin_unlock_irqrestore(&worker->lock, flags);
  266. out:
  267. return 0;
  268. }
  269. /*
  270. * places a struct btrfs_work into the pending queue of one of the kthreads
  271. */
  272. int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
  273. {
  274. struct btrfs_worker_thread *worker;
  275. unsigned long flags;
  276. int wake = 0;
  277. /* don't requeue something already on a list */
  278. if (test_and_set_bit(0, &work->flags))
  279. goto out;
  280. worker = find_worker(workers);
  281. spin_lock_irqsave(&worker->lock, flags);
  282. atomic_inc(&worker->num_pending);
  283. check_busy_worker(worker);
  284. list_add_tail(&work->list, &worker->pending);
  285. /*
  286. * avoid calling into wake_up_process if this thread has already
  287. * been kicked
  288. */
  289. if (!worker->working)
  290. wake = 1;
  291. worker->working = 1;
  292. spin_unlock_irqrestore(&worker->lock, flags);
  293. if (wake)
  294. wake_up_process(worker->task);
  295. out:
  296. return 0;
  297. }