async-thread.c 8.1 KB

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