async-thread.c 11 KB

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