async-thread.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <linux/workqueue.h>
  25. #include "async-thread.h"
  26. #include "ctree.h"
  27. #define WORK_DONE_BIT 0
  28. #define WORK_ORDER_DONE_BIT 1
  29. #define WORK_HIGH_PRIO_BIT 2
  30. #define NO_THRESHOLD (-1)
  31. #define DFT_THRESHOLD (32)
  32. struct __btrfs_workqueue {
  33. struct workqueue_struct *normal_wq;
  34. /* List head pointing to ordered work list */
  35. struct list_head ordered_list;
  36. /* Spinlock for ordered_list */
  37. spinlock_t list_lock;
  38. /* Thresholding related variants */
  39. atomic_t pending;
  40. int max_active;
  41. int current_max;
  42. int thresh;
  43. unsigned int count;
  44. spinlock_t thres_lock;
  45. };
  46. struct btrfs_workqueue {
  47. struct __btrfs_workqueue *normal;
  48. struct __btrfs_workqueue *high;
  49. };
  50. static inline struct __btrfs_workqueue
  51. *__btrfs_alloc_workqueue(const char *name, int flags, int max_active,
  52. int thresh)
  53. {
  54. struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
  55. if (unlikely(!ret))
  56. return NULL;
  57. ret->max_active = max_active;
  58. atomic_set(&ret->pending, 0);
  59. if (thresh == 0)
  60. thresh = DFT_THRESHOLD;
  61. /* For low threshold, disabling threshold is a better choice */
  62. if (thresh < DFT_THRESHOLD) {
  63. ret->current_max = max_active;
  64. ret->thresh = NO_THRESHOLD;
  65. } else {
  66. ret->current_max = 1;
  67. ret->thresh = thresh;
  68. }
  69. if (flags & WQ_HIGHPRI)
  70. ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
  71. ret->max_active,
  72. "btrfs", name);
  73. else
  74. ret->normal_wq = alloc_workqueue("%s-%s", flags,
  75. ret->max_active, "btrfs",
  76. name);
  77. if (unlikely(!ret->normal_wq)) {
  78. kfree(ret);
  79. return NULL;
  80. }
  81. INIT_LIST_HEAD(&ret->ordered_list);
  82. spin_lock_init(&ret->list_lock);
  83. spin_lock_init(&ret->thres_lock);
  84. trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
  85. return ret;
  86. }
  87. static inline void
  88. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
  89. struct btrfs_workqueue *btrfs_alloc_workqueue(const char *name,
  90. int flags,
  91. int max_active,
  92. int thresh)
  93. {
  94. struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
  95. if (unlikely(!ret))
  96. return NULL;
  97. ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
  98. max_active, thresh);
  99. if (unlikely(!ret->normal)) {
  100. kfree(ret);
  101. return NULL;
  102. }
  103. if (flags & WQ_HIGHPRI) {
  104. ret->high = __btrfs_alloc_workqueue(name, flags, max_active,
  105. thresh);
  106. if (unlikely(!ret->high)) {
  107. __btrfs_destroy_workqueue(ret->normal);
  108. kfree(ret);
  109. return NULL;
  110. }
  111. }
  112. return ret;
  113. }
  114. /*
  115. * Hook for threshold which will be called in btrfs_queue_work.
  116. * This hook WILL be called in IRQ handler context,
  117. * so workqueue_set_max_active MUST NOT be called in this hook
  118. */
  119. static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
  120. {
  121. if (wq->thresh == NO_THRESHOLD)
  122. return;
  123. atomic_inc(&wq->pending);
  124. }
  125. /*
  126. * Hook for threshold which will be called before executing the work,
  127. * This hook is called in kthread content.
  128. * So workqueue_set_max_active is called here.
  129. */
  130. static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
  131. {
  132. int new_max_active;
  133. long pending;
  134. int need_change = 0;
  135. if (wq->thresh == NO_THRESHOLD)
  136. return;
  137. atomic_dec(&wq->pending);
  138. spin_lock(&wq->thres_lock);
  139. /*
  140. * Use wq->count to limit the calling frequency of
  141. * workqueue_set_max_active.
  142. */
  143. wq->count++;
  144. wq->count %= (wq->thresh / 4);
  145. if (!wq->count)
  146. goto out;
  147. new_max_active = wq->current_max;
  148. /*
  149. * pending may be changed later, but it's OK since we really
  150. * don't need it so accurate to calculate new_max_active.
  151. */
  152. pending = atomic_read(&wq->pending);
  153. if (pending > wq->thresh)
  154. new_max_active++;
  155. if (pending < wq->thresh / 2)
  156. new_max_active--;
  157. new_max_active = clamp_val(new_max_active, 1, wq->max_active);
  158. if (new_max_active != wq->current_max) {
  159. need_change = 1;
  160. wq->current_max = new_max_active;
  161. }
  162. out:
  163. spin_unlock(&wq->thres_lock);
  164. if (need_change) {
  165. workqueue_set_max_active(wq->normal_wq, wq->current_max);
  166. }
  167. }
  168. static void run_ordered_work(struct __btrfs_workqueue *wq)
  169. {
  170. struct list_head *list = &wq->ordered_list;
  171. struct btrfs_work *work;
  172. spinlock_t *lock = &wq->list_lock;
  173. unsigned long flags;
  174. while (1) {
  175. spin_lock_irqsave(lock, flags);
  176. if (list_empty(list))
  177. break;
  178. work = list_entry(list->next, struct btrfs_work,
  179. ordered_list);
  180. if (!test_bit(WORK_DONE_BIT, &work->flags))
  181. break;
  182. /*
  183. * we are going to call the ordered done function, but
  184. * we leave the work item on the list as a barrier so
  185. * that later work items that are done don't have their
  186. * functions called before this one returns
  187. */
  188. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  189. break;
  190. trace_btrfs_ordered_sched(work);
  191. spin_unlock_irqrestore(lock, flags);
  192. work->ordered_func(work);
  193. /* now take the lock again and drop our item from the list */
  194. spin_lock_irqsave(lock, flags);
  195. list_del(&work->ordered_list);
  196. spin_unlock_irqrestore(lock, flags);
  197. /*
  198. * we don't want to call the ordered free functions
  199. * with the lock held though
  200. */
  201. work->ordered_free(work);
  202. trace_btrfs_all_work_done(work);
  203. }
  204. spin_unlock_irqrestore(lock, flags);
  205. }
  206. static void normal_work_helper(struct work_struct *arg)
  207. {
  208. struct btrfs_work *work;
  209. struct __btrfs_workqueue *wq;
  210. int need_order = 0;
  211. work = container_of(arg, struct btrfs_work, normal_work);
  212. /*
  213. * We should not touch things inside work in the following cases:
  214. * 1) after work->func() if it has no ordered_free
  215. * Since the struct is freed in work->func().
  216. * 2) after setting WORK_DONE_BIT
  217. * The work may be freed in other threads almost instantly.
  218. * So we save the needed things here.
  219. */
  220. if (work->ordered_func)
  221. need_order = 1;
  222. wq = work->wq;
  223. trace_btrfs_work_sched(work);
  224. thresh_exec_hook(wq);
  225. work->func(work);
  226. if (need_order) {
  227. set_bit(WORK_DONE_BIT, &work->flags);
  228. run_ordered_work(wq);
  229. }
  230. if (!need_order)
  231. trace_btrfs_all_work_done(work);
  232. }
  233. void btrfs_init_work(struct btrfs_work *work,
  234. btrfs_func_t func,
  235. btrfs_func_t ordered_func,
  236. btrfs_func_t ordered_free)
  237. {
  238. work->func = func;
  239. work->ordered_func = ordered_func;
  240. work->ordered_free = ordered_free;
  241. INIT_WORK(&work->normal_work, normal_work_helper);
  242. INIT_LIST_HEAD(&work->ordered_list);
  243. work->flags = 0;
  244. }
  245. static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
  246. struct btrfs_work *work)
  247. {
  248. unsigned long flags;
  249. work->wq = wq;
  250. thresh_queue_hook(wq);
  251. if (work->ordered_func) {
  252. spin_lock_irqsave(&wq->list_lock, flags);
  253. list_add_tail(&work->ordered_list, &wq->ordered_list);
  254. spin_unlock_irqrestore(&wq->list_lock, flags);
  255. }
  256. queue_work(wq->normal_wq, &work->normal_work);
  257. trace_btrfs_work_queued(work);
  258. }
  259. void btrfs_queue_work(struct btrfs_workqueue *wq,
  260. struct btrfs_work *work)
  261. {
  262. struct __btrfs_workqueue *dest_wq;
  263. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
  264. dest_wq = wq->high;
  265. else
  266. dest_wq = wq->normal;
  267. __btrfs_queue_work(dest_wq, work);
  268. }
  269. static inline void
  270. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
  271. {
  272. destroy_workqueue(wq->normal_wq);
  273. trace_btrfs_workqueue_destroy(wq);
  274. kfree(wq);
  275. }
  276. void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
  277. {
  278. if (!wq)
  279. return;
  280. if (wq->high)
  281. __btrfs_destroy_workqueue(wq->high);
  282. __btrfs_destroy_workqueue(wq->normal);
  283. kfree(wq);
  284. }
  285. void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int max)
  286. {
  287. if (!wq)
  288. return;
  289. wq->normal->max_active = max;
  290. if (wq->high)
  291. wq->high->max_active = max;
  292. }
  293. void btrfs_set_work_high_priority(struct btrfs_work *work)
  294. {
  295. set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
  296. }