async-thread.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. * Copyright (C) 2014 Fujitsu. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 021110-1307, USA.
  18. */
  19. #include <linux/kthread.h>
  20. #include <linux/slab.h>
  21. #include <linux/list.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/freezer.h>
  24. #include "async-thread.h"
  25. #include "ctree.h"
  26. #define WORK_DONE_BIT 0
  27. #define WORK_ORDER_DONE_BIT 1
  28. #define WORK_HIGH_PRIO_BIT 2
  29. #define NO_THRESHOLD (-1)
  30. #define DFT_THRESHOLD (32)
  31. struct __btrfs_workqueue {
  32. struct workqueue_struct *normal_wq;
  33. /* List head pointing to ordered work list */
  34. struct list_head ordered_list;
  35. /* Spinlock for ordered_list */
  36. spinlock_t list_lock;
  37. /* Thresholding related variants */
  38. atomic_t pending;
  39. int max_active;
  40. int current_max;
  41. int thresh;
  42. unsigned int count;
  43. spinlock_t thres_lock;
  44. };
  45. struct btrfs_workqueue {
  46. struct __btrfs_workqueue *normal;
  47. struct __btrfs_workqueue *high;
  48. };
  49. static void normal_work_helper(struct btrfs_work *work);
  50. #define BTRFS_WORK_HELPER(name) \
  51. void btrfs_##name(struct work_struct *arg) \
  52. { \
  53. struct btrfs_work *work = container_of(arg, struct btrfs_work, \
  54. normal_work); \
  55. normal_work_helper(work); \
  56. }
  57. BTRFS_WORK_HELPER(worker_helper);
  58. BTRFS_WORK_HELPER(delalloc_helper);
  59. BTRFS_WORK_HELPER(flush_delalloc_helper);
  60. BTRFS_WORK_HELPER(cache_helper);
  61. BTRFS_WORK_HELPER(submit_helper);
  62. BTRFS_WORK_HELPER(fixup_helper);
  63. BTRFS_WORK_HELPER(endio_helper);
  64. BTRFS_WORK_HELPER(endio_meta_helper);
  65. BTRFS_WORK_HELPER(endio_meta_write_helper);
  66. BTRFS_WORK_HELPER(endio_raid56_helper);
  67. BTRFS_WORK_HELPER(endio_repair_helper);
  68. BTRFS_WORK_HELPER(rmw_helper);
  69. BTRFS_WORK_HELPER(endio_write_helper);
  70. BTRFS_WORK_HELPER(freespace_write_helper);
  71. BTRFS_WORK_HELPER(delayed_meta_helper);
  72. BTRFS_WORK_HELPER(readahead_helper);
  73. BTRFS_WORK_HELPER(qgroup_rescan_helper);
  74. BTRFS_WORK_HELPER(extent_refs_helper);
  75. BTRFS_WORK_HELPER(scrub_helper);
  76. BTRFS_WORK_HELPER(scrubwrc_helper);
  77. BTRFS_WORK_HELPER(scrubnc_helper);
  78. BTRFS_WORK_HELPER(scrubparity_helper);
  79. static struct __btrfs_workqueue *
  80. __btrfs_alloc_workqueue(const char *name, unsigned int flags, int max_active,
  81. int thresh)
  82. {
  83. struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
  84. if (!ret)
  85. return NULL;
  86. ret->max_active = max_active;
  87. atomic_set(&ret->pending, 0);
  88. if (thresh == 0)
  89. thresh = DFT_THRESHOLD;
  90. /* For low threshold, disabling threshold is a better choice */
  91. if (thresh < DFT_THRESHOLD) {
  92. ret->current_max = max_active;
  93. ret->thresh = NO_THRESHOLD;
  94. } else {
  95. ret->current_max = 1;
  96. ret->thresh = thresh;
  97. }
  98. if (flags & WQ_HIGHPRI)
  99. ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
  100. ret->max_active,
  101. "btrfs", name);
  102. else
  103. ret->normal_wq = alloc_workqueue("%s-%s", flags,
  104. ret->max_active, "btrfs",
  105. name);
  106. if (!ret->normal_wq) {
  107. kfree(ret);
  108. return NULL;
  109. }
  110. INIT_LIST_HEAD(&ret->ordered_list);
  111. spin_lock_init(&ret->list_lock);
  112. spin_lock_init(&ret->thres_lock);
  113. trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
  114. return ret;
  115. }
  116. static inline void
  117. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
  118. struct btrfs_workqueue *btrfs_alloc_workqueue(const char *name,
  119. unsigned int flags,
  120. int max_active,
  121. int thresh)
  122. {
  123. struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
  124. if (!ret)
  125. return NULL;
  126. ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
  127. max_active, thresh);
  128. if (!ret->normal) {
  129. kfree(ret);
  130. return NULL;
  131. }
  132. if (flags & WQ_HIGHPRI) {
  133. ret->high = __btrfs_alloc_workqueue(name, flags, max_active,
  134. thresh);
  135. if (!ret->high) {
  136. __btrfs_destroy_workqueue(ret->normal);
  137. kfree(ret);
  138. return NULL;
  139. }
  140. }
  141. return ret;
  142. }
  143. /*
  144. * Hook for threshold which will be called in btrfs_queue_work.
  145. * This hook WILL be called in IRQ handler context,
  146. * so workqueue_set_max_active MUST NOT be called in this hook
  147. */
  148. static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
  149. {
  150. if (wq->thresh == NO_THRESHOLD)
  151. return;
  152. atomic_inc(&wq->pending);
  153. }
  154. /*
  155. * Hook for threshold which will be called before executing the work,
  156. * This hook is called in kthread content.
  157. * So workqueue_set_max_active is called here.
  158. */
  159. static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
  160. {
  161. int new_max_active;
  162. long pending;
  163. int need_change = 0;
  164. if (wq->thresh == NO_THRESHOLD)
  165. return;
  166. atomic_dec(&wq->pending);
  167. spin_lock(&wq->thres_lock);
  168. /*
  169. * Use wq->count to limit the calling frequency of
  170. * workqueue_set_max_active.
  171. */
  172. wq->count++;
  173. wq->count %= (wq->thresh / 4);
  174. if (!wq->count)
  175. goto out;
  176. new_max_active = wq->current_max;
  177. /*
  178. * pending may be changed later, but it's OK since we really
  179. * don't need it so accurate to calculate new_max_active.
  180. */
  181. pending = atomic_read(&wq->pending);
  182. if (pending > wq->thresh)
  183. new_max_active++;
  184. if (pending < wq->thresh / 2)
  185. new_max_active--;
  186. new_max_active = clamp_val(new_max_active, 1, wq->max_active);
  187. if (new_max_active != wq->current_max) {
  188. need_change = 1;
  189. wq->current_max = new_max_active;
  190. }
  191. out:
  192. spin_unlock(&wq->thres_lock);
  193. if (need_change) {
  194. workqueue_set_max_active(wq->normal_wq, wq->current_max);
  195. }
  196. }
  197. static void run_ordered_work(struct __btrfs_workqueue *wq)
  198. {
  199. struct list_head *list = &wq->ordered_list;
  200. struct btrfs_work *work;
  201. spinlock_t *lock = &wq->list_lock;
  202. unsigned long flags;
  203. while (1) {
  204. spin_lock_irqsave(lock, flags);
  205. if (list_empty(list))
  206. break;
  207. work = list_entry(list->next, struct btrfs_work,
  208. ordered_list);
  209. if (!test_bit(WORK_DONE_BIT, &work->flags))
  210. break;
  211. /*
  212. * we are going to call the ordered done function, but
  213. * we leave the work item on the list as a barrier so
  214. * that later work items that are done don't have their
  215. * functions called before this one returns
  216. */
  217. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  218. break;
  219. trace_btrfs_ordered_sched(work);
  220. spin_unlock_irqrestore(lock, flags);
  221. work->ordered_func(work);
  222. /* now take the lock again and drop our item from the list */
  223. spin_lock_irqsave(lock, flags);
  224. list_del(&work->ordered_list);
  225. spin_unlock_irqrestore(lock, flags);
  226. /*
  227. * we don't want to call the ordered free functions
  228. * with the lock held though
  229. */
  230. work->ordered_free(work);
  231. trace_btrfs_all_work_done(work);
  232. }
  233. spin_unlock_irqrestore(lock, flags);
  234. }
  235. static void normal_work_helper(struct btrfs_work *work)
  236. {
  237. struct __btrfs_workqueue *wq;
  238. int need_order = 0;
  239. /*
  240. * We should not touch things inside work in the following cases:
  241. * 1) after work->func() if it has no ordered_free
  242. * Since the struct is freed in work->func().
  243. * 2) after setting WORK_DONE_BIT
  244. * The work may be freed in other threads almost instantly.
  245. * So we save the needed things here.
  246. */
  247. if (work->ordered_func)
  248. need_order = 1;
  249. wq = work->wq;
  250. trace_btrfs_work_sched(work);
  251. thresh_exec_hook(wq);
  252. work->func(work);
  253. if (need_order) {
  254. set_bit(WORK_DONE_BIT, &work->flags);
  255. run_ordered_work(wq);
  256. }
  257. if (!need_order)
  258. trace_btrfs_all_work_done(work);
  259. }
  260. void btrfs_init_work(struct btrfs_work *work, btrfs_work_func_t uniq_func,
  261. btrfs_func_t func,
  262. btrfs_func_t ordered_func,
  263. btrfs_func_t ordered_free)
  264. {
  265. work->func = func;
  266. work->ordered_func = ordered_func;
  267. work->ordered_free = ordered_free;
  268. INIT_WORK(&work->normal_work, uniq_func);
  269. INIT_LIST_HEAD(&work->ordered_list);
  270. work->flags = 0;
  271. }
  272. static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
  273. struct btrfs_work *work)
  274. {
  275. unsigned long flags;
  276. work->wq = wq;
  277. thresh_queue_hook(wq);
  278. if (work->ordered_func) {
  279. spin_lock_irqsave(&wq->list_lock, flags);
  280. list_add_tail(&work->ordered_list, &wq->ordered_list);
  281. spin_unlock_irqrestore(&wq->list_lock, flags);
  282. }
  283. queue_work(wq->normal_wq, &work->normal_work);
  284. trace_btrfs_work_queued(work);
  285. }
  286. void btrfs_queue_work(struct btrfs_workqueue *wq,
  287. struct btrfs_work *work)
  288. {
  289. struct __btrfs_workqueue *dest_wq;
  290. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
  291. dest_wq = wq->high;
  292. else
  293. dest_wq = wq->normal;
  294. __btrfs_queue_work(dest_wq, work);
  295. }
  296. static inline void
  297. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
  298. {
  299. destroy_workqueue(wq->normal_wq);
  300. trace_btrfs_workqueue_destroy(wq);
  301. kfree(wq);
  302. }
  303. void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
  304. {
  305. if (!wq)
  306. return;
  307. if (wq->high)
  308. __btrfs_destroy_workqueue(wq->high);
  309. __btrfs_destroy_workqueue(wq->normal);
  310. kfree(wq);
  311. }
  312. void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int max)
  313. {
  314. if (!wq)
  315. return;
  316. wq->normal->max_active = max;
  317. if (wq->high)
  318. wq->high->max_active = max;
  319. }
  320. void btrfs_set_work_high_priority(struct btrfs_work *work)
  321. {
  322. set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
  323. }