async-thread.c 7.9 KB

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