async-thread.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /* list of struct btrfs_work that are waiting for service */
  34. struct list_head pending;
  35. /* list of worker threads from struct btrfs_workers */
  36. struct list_head worker_list;
  37. /* kthread */
  38. struct task_struct *task;
  39. /* number of things on the pending list */
  40. atomic_t num_pending;
  41. /* protects the pending list. */
  42. spinlock_t lock;
  43. /* set to non-zero when this thread is already awake and kicking */
  44. int working;
  45. };
  46. /*
  47. * main loop for servicing work items
  48. */
  49. static int worker_loop(void *arg)
  50. {
  51. struct btrfs_worker_thread *worker = arg;
  52. struct list_head *cur;
  53. struct btrfs_work *work;
  54. do {
  55. spin_lock_irq(&worker->lock);
  56. while(!list_empty(&worker->pending)) {
  57. cur = worker->pending.next;
  58. work = list_entry(cur, struct btrfs_work, list);
  59. list_del(&work->list);
  60. clear_bit(0, &work->flags);
  61. work->worker = worker;
  62. spin_unlock_irq(&worker->lock);
  63. work->func(work);
  64. atomic_dec(&worker->num_pending);
  65. spin_lock_irq(&worker->lock);
  66. }
  67. worker->working = 0;
  68. if (freezing(current)) {
  69. refrigerator();
  70. } else {
  71. set_current_state(TASK_INTERRUPTIBLE);
  72. spin_unlock_irq(&worker->lock);
  73. schedule();
  74. __set_current_state(TASK_RUNNING);
  75. }
  76. } while (!kthread_should_stop());
  77. return 0;
  78. }
  79. /*
  80. * this will wait for all the worker threads to shutdown
  81. */
  82. int btrfs_stop_workers(struct btrfs_workers *workers)
  83. {
  84. struct list_head *cur;
  85. struct btrfs_worker_thread *worker;
  86. while(!list_empty(&workers->worker_list)) {
  87. cur = workers->worker_list.next;
  88. worker = list_entry(cur, struct btrfs_worker_thread,
  89. worker_list);
  90. kthread_stop(worker->task);
  91. list_del(&worker->worker_list);
  92. kfree(worker);
  93. }
  94. return 0;
  95. }
  96. /*
  97. * simple init on struct btrfs_workers
  98. */
  99. void btrfs_init_workers(struct btrfs_workers *workers, int max)
  100. {
  101. workers->num_workers = 0;
  102. INIT_LIST_HEAD(&workers->worker_list);
  103. workers->last = NULL;
  104. spin_lock_init(&workers->lock);
  105. workers->max_workers = max;
  106. }
  107. /*
  108. * starts new worker threads. This does not enforce the max worker
  109. * count in case you need to temporarily go past it.
  110. */
  111. int btrfs_start_workers(struct btrfs_workers *workers, int num_workers)
  112. {
  113. struct btrfs_worker_thread *worker;
  114. int ret = 0;
  115. int i;
  116. for (i = 0; i < num_workers; i++) {
  117. worker = kzalloc(sizeof(*worker), GFP_NOFS);
  118. if (!worker) {
  119. ret = -ENOMEM;
  120. goto fail;
  121. }
  122. INIT_LIST_HEAD(&worker->pending);
  123. INIT_LIST_HEAD(&worker->worker_list);
  124. spin_lock_init(&worker->lock);
  125. atomic_set(&worker->num_pending, 0);
  126. worker->task = kthread_run(worker_loop, worker, "btrfs");
  127. if (IS_ERR(worker->task)) {
  128. ret = PTR_ERR(worker->task);
  129. goto fail;
  130. }
  131. spin_lock_irq(&workers->lock);
  132. list_add_tail(&worker->worker_list, &workers->worker_list);
  133. workers->last = worker;
  134. workers->num_workers++;
  135. spin_unlock_irq(&workers->lock);
  136. }
  137. return 0;
  138. fail:
  139. btrfs_stop_workers(workers);
  140. return ret;
  141. }
  142. /*
  143. * run through the list and find a worker thread that doesn't have a lot
  144. * to do right now. This can return null if we aren't yet at the thread
  145. * count limit and all of the threads are busy.
  146. */
  147. static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
  148. {
  149. struct btrfs_worker_thread *worker;
  150. struct list_head *next;
  151. struct list_head *start;
  152. int enforce_min = workers->num_workers < workers->max_workers;
  153. /* start with the last thread if it isn't busy */
  154. worker = workers->last;
  155. if (atomic_read(&worker->num_pending) < 64)
  156. goto done;
  157. next = worker->worker_list.next;
  158. start = &worker->worker_list;
  159. /*
  160. * check all the workers for someone that is bored. FIXME, do
  161. * something smart here
  162. */
  163. while(next != start) {
  164. if (next == &workers->worker_list) {
  165. next = workers->worker_list.next;
  166. continue;
  167. }
  168. worker = list_entry(next, struct btrfs_worker_thread,
  169. worker_list);
  170. if (atomic_read(&worker->num_pending) < 64 || !enforce_min)
  171. goto done;
  172. next = next->next;
  173. }
  174. /*
  175. * nobody was bored, if we're already at the max thread count,
  176. * use the last thread
  177. */
  178. if (!enforce_min || atomic_read(&workers->last->num_pending) < 64) {
  179. return workers->last;
  180. }
  181. return NULL;
  182. done:
  183. workers->last = worker;
  184. return worker;
  185. }
  186. static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
  187. {
  188. struct btrfs_worker_thread *worker;
  189. unsigned long flags;
  190. again:
  191. spin_lock_irqsave(&workers->lock, flags);
  192. worker = next_worker(workers);
  193. spin_unlock_irqrestore(&workers->lock, flags);
  194. if (!worker) {
  195. spin_lock_irqsave(&workers->lock, flags);
  196. if (workers->num_workers >= workers->max_workers) {
  197. /*
  198. * we have failed to find any workers, just
  199. * return the force one
  200. */
  201. worker = list_entry(workers->worker_list.next,
  202. struct btrfs_worker_thread, worker_list);
  203. spin_unlock_irqrestore(&workers->lock, flags);
  204. } else {
  205. spin_unlock_irqrestore(&workers->lock, flags);
  206. /* we're below the limit, start another worker */
  207. btrfs_start_workers(workers, 1);
  208. goto again;
  209. }
  210. }
  211. return worker;
  212. }
  213. /*
  214. * btrfs_requeue_work just puts the work item back on the tail of the list
  215. * it was taken from. It is intended for use with long running work functions
  216. * that make some progress and want to give the cpu up for others.
  217. */
  218. int btrfs_requeue_work(struct btrfs_work *work)
  219. {
  220. struct btrfs_worker_thread *worker = work->worker;
  221. unsigned long flags;
  222. if (test_and_set_bit(0, &work->flags))
  223. goto out;
  224. spin_lock_irqsave(&worker->lock, flags);
  225. atomic_inc(&worker->num_pending);
  226. list_add_tail(&work->list, &worker->pending);
  227. spin_unlock_irqrestore(&worker->lock, flags);
  228. out:
  229. return 0;
  230. }
  231. /*
  232. * places a struct btrfs_work into the pending queue of one of the kthreads
  233. */
  234. int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
  235. {
  236. struct btrfs_worker_thread *worker;
  237. unsigned long flags;
  238. int wake = 0;
  239. /* don't requeue something already on a list */
  240. if (test_and_set_bit(0, &work->flags))
  241. goto out;
  242. worker = find_worker(workers);
  243. spin_lock_irqsave(&worker->lock, flags);
  244. atomic_inc(&worker->num_pending);
  245. list_add_tail(&work->list, &worker->pending);
  246. /*
  247. * avoid calling into wake_up_process if this thread has already
  248. * been kicked
  249. */
  250. if (!worker->working)
  251. wake = 1;
  252. worker->working = 1;
  253. spin_unlock_irqrestore(&worker->lock, flags);
  254. if (wake)
  255. wake_up_process(worker->task);
  256. out:
  257. return 0;
  258. }