async-thread.c 12 KB

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