async-thread.c 15 KB

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