async-thread.c 18 KB

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