async-thread.c 9.4 KB

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