async-thread.c 12 KB

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