async-thread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. again_locked:
  121. while (!list_empty(&worker->pending)) {
  122. cur = worker->pending.next;
  123. work = list_entry(cur, struct btrfs_work, list);
  124. list_del(&work->list);
  125. clear_bit(WORK_QUEUED_BIT, &work->flags);
  126. work->worker = worker;
  127. spin_unlock_irq(&worker->lock);
  128. work->func(work);
  129. atomic_dec(&worker->num_pending);
  130. /*
  131. * unless this is an ordered work queue,
  132. * 'work' was probably freed by func above.
  133. */
  134. run_ordered_completions(worker->workers, work);
  135. spin_lock_irq(&worker->lock);
  136. check_idle_worker(worker);
  137. }
  138. if (freezing(current)) {
  139. worker->working = 0;
  140. spin_unlock_irq(&worker->lock);
  141. refrigerator();
  142. } else {
  143. spin_unlock_irq(&worker->lock);
  144. if (!kthread_should_stop()) {
  145. cpu_relax();
  146. /*
  147. * we've dropped the lock, did someone else
  148. * jump_in?
  149. */
  150. smp_mb();
  151. if (!list_empty(&worker->pending))
  152. continue;
  153. /*
  154. * this short schedule allows more work to
  155. * come in without the queue functions
  156. * needing to go through wake_up_process()
  157. *
  158. * worker->working is still 1, so nobody
  159. * is going to try and wake us up
  160. */
  161. schedule_timeout(1);
  162. smp_mb();
  163. if (!list_empty(&worker->pending))
  164. continue;
  165. if (kthread_should_stop())
  166. break;
  167. /* still no more work?, sleep for real */
  168. spin_lock_irq(&worker->lock);
  169. set_current_state(TASK_INTERRUPTIBLE);
  170. if (!list_empty(&worker->pending))
  171. goto again_locked;
  172. /*
  173. * this makes sure we get a wakeup when someone
  174. * adds something new to the queue
  175. */
  176. worker->working = 0;
  177. spin_unlock_irq(&worker->lock);
  178. if (!kthread_should_stop())
  179. schedule();
  180. }
  181. __set_current_state(TASK_RUNNING);
  182. }
  183. } while (!kthread_should_stop());
  184. return 0;
  185. }
  186. /*
  187. * this will wait for all the worker threads to shutdown
  188. */
  189. int btrfs_stop_workers(struct btrfs_workers *workers)
  190. {
  191. struct list_head *cur;
  192. struct btrfs_worker_thread *worker;
  193. list_splice_init(&workers->idle_list, &workers->worker_list);
  194. while (!list_empty(&workers->worker_list)) {
  195. cur = workers->worker_list.next;
  196. worker = list_entry(cur, struct btrfs_worker_thread,
  197. worker_list);
  198. kthread_stop(worker->task);
  199. list_del(&worker->worker_list);
  200. kfree(worker);
  201. }
  202. return 0;
  203. }
  204. /*
  205. * simple init on struct btrfs_workers
  206. */
  207. void btrfs_init_workers(struct btrfs_workers *workers, char *name, int max)
  208. {
  209. workers->num_workers = 0;
  210. INIT_LIST_HEAD(&workers->worker_list);
  211. INIT_LIST_HEAD(&workers->idle_list);
  212. INIT_LIST_HEAD(&workers->order_list);
  213. spin_lock_init(&workers->lock);
  214. workers->max_workers = max;
  215. workers->idle_thresh = 32;
  216. workers->name = name;
  217. workers->ordered = 0;
  218. }
  219. /*
  220. * starts new worker threads. This does not enforce the max worker
  221. * count in case you need to temporarily go past it.
  222. */
  223. int btrfs_start_workers(struct btrfs_workers *workers, int num_workers)
  224. {
  225. struct btrfs_worker_thread *worker;
  226. int ret = 0;
  227. int i;
  228. for (i = 0; i < num_workers; i++) {
  229. worker = kzalloc(sizeof(*worker), GFP_NOFS);
  230. if (!worker) {
  231. ret = -ENOMEM;
  232. goto fail;
  233. }
  234. INIT_LIST_HEAD(&worker->pending);
  235. INIT_LIST_HEAD(&worker->worker_list);
  236. spin_lock_init(&worker->lock);
  237. atomic_set(&worker->num_pending, 0);
  238. worker->task = kthread_run(worker_loop, worker,
  239. "btrfs-%s-%d", workers->name,
  240. workers->num_workers + i);
  241. worker->workers = workers;
  242. if (IS_ERR(worker->task)) {
  243. kfree(worker);
  244. ret = PTR_ERR(worker->task);
  245. goto fail;
  246. }
  247. spin_lock_irq(&workers->lock);
  248. list_add_tail(&worker->worker_list, &workers->idle_list);
  249. worker->idle = 1;
  250. workers->num_workers++;
  251. spin_unlock_irq(&workers->lock);
  252. }
  253. return 0;
  254. fail:
  255. btrfs_stop_workers(workers);
  256. return ret;
  257. }
  258. /*
  259. * run through the list and find a worker thread that doesn't have a lot
  260. * to do right now. This can return null if we aren't yet at the thread
  261. * count limit and all of the threads are busy.
  262. */
  263. static struct btrfs_worker_thread *next_worker(struct btrfs_workers *workers)
  264. {
  265. struct btrfs_worker_thread *worker;
  266. struct list_head *next;
  267. int enforce_min = workers->num_workers < workers->max_workers;
  268. /*
  269. * if we find an idle thread, don't move it to the end of the
  270. * idle list. This improves the chance that the next submission
  271. * will reuse the same thread, and maybe catch it while it is still
  272. * working
  273. */
  274. if (!list_empty(&workers->idle_list)) {
  275. next = workers->idle_list.next;
  276. worker = list_entry(next, struct btrfs_worker_thread,
  277. worker_list);
  278. return worker;
  279. }
  280. if (enforce_min || list_empty(&workers->worker_list))
  281. return NULL;
  282. /*
  283. * if we pick a busy task, move the task to the end of the list.
  284. * hopefully this will keep things somewhat evenly balanced.
  285. * Do the move in batches based on the sequence number. This groups
  286. * requests submitted at roughly the same time onto the same worker.
  287. */
  288. next = workers->worker_list.next;
  289. worker = list_entry(next, struct btrfs_worker_thread, worker_list);
  290. atomic_inc(&worker->num_pending);
  291. worker->sequence++;
  292. if (worker->sequence % workers->idle_thresh == 0)
  293. list_move_tail(next, &workers->worker_list);
  294. return worker;
  295. }
  296. /*
  297. * selects a worker thread to take the next job. This will either find
  298. * an idle worker, start a new worker up to the max count, or just return
  299. * one of the existing busy workers.
  300. */
  301. static struct btrfs_worker_thread *find_worker(struct btrfs_workers *workers)
  302. {
  303. struct btrfs_worker_thread *worker;
  304. unsigned long flags;
  305. again:
  306. spin_lock_irqsave(&workers->lock, flags);
  307. worker = next_worker(workers);
  308. spin_unlock_irqrestore(&workers->lock, flags);
  309. if (!worker) {
  310. spin_lock_irqsave(&workers->lock, flags);
  311. if (workers->num_workers >= workers->max_workers) {
  312. struct list_head *fallback = NULL;
  313. /*
  314. * we have failed to find any workers, just
  315. * return the force one
  316. */
  317. if (!list_empty(&workers->worker_list))
  318. fallback = workers->worker_list.next;
  319. if (!list_empty(&workers->idle_list))
  320. fallback = workers->idle_list.next;
  321. BUG_ON(!fallback);
  322. worker = list_entry(fallback,
  323. struct btrfs_worker_thread, worker_list);
  324. spin_unlock_irqrestore(&workers->lock, flags);
  325. } else {
  326. spin_unlock_irqrestore(&workers->lock, flags);
  327. /* we're below the limit, start another worker */
  328. btrfs_start_workers(workers, 1);
  329. goto again;
  330. }
  331. }
  332. return worker;
  333. }
  334. /*
  335. * btrfs_requeue_work just puts the work item back on the tail of the list
  336. * it was taken from. It is intended for use with long running work functions
  337. * that make some progress and want to give the cpu up for others.
  338. */
  339. int btrfs_requeue_work(struct btrfs_work *work)
  340. {
  341. struct btrfs_worker_thread *worker = work->worker;
  342. unsigned long flags;
  343. int wake = 0;
  344. if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
  345. goto out;
  346. spin_lock_irqsave(&worker->lock, flags);
  347. list_add_tail(&work->list, &worker->pending);
  348. atomic_inc(&worker->num_pending);
  349. /* by definition we're busy, take ourselves off the idle
  350. * list
  351. */
  352. if (worker->idle) {
  353. spin_lock_irqsave(&worker->workers->lock, flags);
  354. worker->idle = 0;
  355. list_move_tail(&worker->worker_list,
  356. &worker->workers->worker_list);
  357. spin_unlock_irqrestore(&worker->workers->lock, flags);
  358. }
  359. if (!worker->working) {
  360. wake = 1;
  361. worker->working = 1;
  362. }
  363. spin_unlock_irqrestore(&worker->lock, flags);
  364. if (wake)
  365. wake_up_process(worker->task);
  366. out:
  367. return 0;
  368. }
  369. /*
  370. * places a struct btrfs_work into the pending queue of one of the kthreads
  371. */
  372. int btrfs_queue_worker(struct btrfs_workers *workers, struct btrfs_work *work)
  373. {
  374. struct btrfs_worker_thread *worker;
  375. unsigned long flags;
  376. int wake = 0;
  377. /* don't requeue something already on a list */
  378. if (test_and_set_bit(WORK_QUEUED_BIT, &work->flags))
  379. goto out;
  380. worker = find_worker(workers);
  381. if (workers->ordered) {
  382. spin_lock_irqsave(&workers->lock, flags);
  383. list_add_tail(&work->order_list, &workers->order_list);
  384. spin_unlock_irqrestore(&workers->lock, flags);
  385. } else {
  386. INIT_LIST_HEAD(&work->order_list);
  387. }
  388. spin_lock_irqsave(&worker->lock, flags);
  389. list_add_tail(&work->list, &worker->pending);
  390. atomic_inc(&worker->num_pending);
  391. check_busy_worker(worker);
  392. /*
  393. * avoid calling into wake_up_process if this thread has already
  394. * been kicked
  395. */
  396. if (!worker->working)
  397. wake = 1;
  398. worker->working = 1;
  399. spin_unlock_irqrestore(&worker->lock, flags);
  400. if (wake)
  401. wake_up_process(worker->task);
  402. out:
  403. return 0;
  404. }