async-thread.c 13 KB

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