async-thread.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /* Up limit of concurrency workers */
  40. int limit_active;
  41. /* Current number of concurrency workers */
  42. int current_active;
  43. /* Threshold to change current_active */
  44. int thresh;
  45. unsigned int count;
  46. spinlock_t thres_lock;
  47. };
  48. struct btrfs_workqueue {
  49. struct __btrfs_workqueue *normal;
  50. struct __btrfs_workqueue *high;
  51. };
  52. static void normal_work_helper(struct btrfs_work *work);
  53. #define BTRFS_WORK_HELPER(name) \
  54. void btrfs_##name(struct work_struct *arg) \
  55. { \
  56. struct btrfs_work *work = container_of(arg, struct btrfs_work, \
  57. normal_work); \
  58. normal_work_helper(work); \
  59. }
  60. bool btrfs_workqueue_normal_congested(struct btrfs_workqueue *wq)
  61. {
  62. /*
  63. * We could compare wq->normal->pending with num_online_cpus()
  64. * to support "thresh == NO_THRESHOLD" case, but it requires
  65. * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's
  66. * postpone it until someone needs the support of that case.
  67. */
  68. if (wq->normal->thresh == NO_THRESHOLD)
  69. return false;
  70. return atomic_read(&wq->normal->pending) > wq->normal->thresh * 2;
  71. }
  72. BTRFS_WORK_HELPER(worker_helper);
  73. BTRFS_WORK_HELPER(delalloc_helper);
  74. BTRFS_WORK_HELPER(flush_delalloc_helper);
  75. BTRFS_WORK_HELPER(cache_helper);
  76. BTRFS_WORK_HELPER(submit_helper);
  77. BTRFS_WORK_HELPER(fixup_helper);
  78. BTRFS_WORK_HELPER(endio_helper);
  79. BTRFS_WORK_HELPER(endio_meta_helper);
  80. BTRFS_WORK_HELPER(endio_meta_write_helper);
  81. BTRFS_WORK_HELPER(endio_raid56_helper);
  82. BTRFS_WORK_HELPER(endio_repair_helper);
  83. BTRFS_WORK_HELPER(rmw_helper);
  84. BTRFS_WORK_HELPER(endio_write_helper);
  85. BTRFS_WORK_HELPER(freespace_write_helper);
  86. BTRFS_WORK_HELPER(delayed_meta_helper);
  87. BTRFS_WORK_HELPER(readahead_helper);
  88. BTRFS_WORK_HELPER(qgroup_rescan_helper);
  89. BTRFS_WORK_HELPER(extent_refs_helper);
  90. BTRFS_WORK_HELPER(scrub_helper);
  91. BTRFS_WORK_HELPER(scrubwrc_helper);
  92. BTRFS_WORK_HELPER(scrubnc_helper);
  93. BTRFS_WORK_HELPER(scrubparity_helper);
  94. static struct __btrfs_workqueue *
  95. __btrfs_alloc_workqueue(const char *name, unsigned int flags, int limit_active,
  96. int thresh)
  97. {
  98. struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
  99. if (!ret)
  100. return NULL;
  101. ret->limit_active = limit_active;
  102. atomic_set(&ret->pending, 0);
  103. if (thresh == 0)
  104. thresh = DFT_THRESHOLD;
  105. /* For low threshold, disabling threshold is a better choice */
  106. if (thresh < DFT_THRESHOLD) {
  107. ret->current_active = limit_active;
  108. ret->thresh = NO_THRESHOLD;
  109. } else {
  110. /*
  111. * For threshold-able wq, let its concurrency grow on demand.
  112. * Use minimal max_active at alloc time to reduce resource
  113. * usage.
  114. */
  115. ret->current_active = 1;
  116. ret->thresh = thresh;
  117. }
  118. if (flags & WQ_HIGHPRI)
  119. ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
  120. ret->current_active, "btrfs",
  121. name);
  122. else
  123. ret->normal_wq = alloc_workqueue("%s-%s", flags,
  124. ret->current_active, "btrfs",
  125. name);
  126. if (!ret->normal_wq) {
  127. kfree(ret);
  128. return NULL;
  129. }
  130. INIT_LIST_HEAD(&ret->ordered_list);
  131. spin_lock_init(&ret->list_lock);
  132. spin_lock_init(&ret->thres_lock);
  133. trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
  134. return ret;
  135. }
  136. static inline void
  137. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
  138. struct btrfs_workqueue *btrfs_alloc_workqueue(const char *name,
  139. unsigned int flags,
  140. int limit_active,
  141. int thresh)
  142. {
  143. struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
  144. if (!ret)
  145. return NULL;
  146. ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
  147. limit_active, thresh);
  148. if (!ret->normal) {
  149. kfree(ret);
  150. return NULL;
  151. }
  152. if (flags & WQ_HIGHPRI) {
  153. ret->high = __btrfs_alloc_workqueue(name, flags, limit_active,
  154. thresh);
  155. if (!ret->high) {
  156. __btrfs_destroy_workqueue(ret->normal);
  157. kfree(ret);
  158. return NULL;
  159. }
  160. }
  161. return ret;
  162. }
  163. /*
  164. * Hook for threshold which will be called in btrfs_queue_work.
  165. * This hook WILL be called in IRQ handler context,
  166. * so workqueue_set_max_active MUST NOT be called in this hook
  167. */
  168. static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
  169. {
  170. if (wq->thresh == NO_THRESHOLD)
  171. return;
  172. atomic_inc(&wq->pending);
  173. }
  174. /*
  175. * Hook for threshold which will be called before executing the work,
  176. * This hook is called in kthread content.
  177. * So workqueue_set_max_active is called here.
  178. */
  179. static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
  180. {
  181. int new_current_active;
  182. long pending;
  183. int need_change = 0;
  184. if (wq->thresh == NO_THRESHOLD)
  185. return;
  186. atomic_dec(&wq->pending);
  187. spin_lock(&wq->thres_lock);
  188. /*
  189. * Use wq->count to limit the calling frequency of
  190. * workqueue_set_max_active.
  191. */
  192. wq->count++;
  193. wq->count %= (wq->thresh / 4);
  194. if (!wq->count)
  195. goto out;
  196. new_current_active = wq->current_active;
  197. /*
  198. * pending may be changed later, but it's OK since we really
  199. * don't need it so accurate to calculate new_max_active.
  200. */
  201. pending = atomic_read(&wq->pending);
  202. if (pending > wq->thresh)
  203. new_current_active++;
  204. if (pending < wq->thresh / 2)
  205. new_current_active--;
  206. new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
  207. if (new_current_active != wq->current_active) {
  208. need_change = 1;
  209. wq->current_active = new_current_active;
  210. }
  211. out:
  212. spin_unlock(&wq->thres_lock);
  213. if (need_change) {
  214. workqueue_set_max_active(wq->normal_wq, wq->current_active);
  215. }
  216. }
  217. static void run_ordered_work(struct __btrfs_workqueue *wq)
  218. {
  219. struct list_head *list = &wq->ordered_list;
  220. struct btrfs_work *work;
  221. spinlock_t *lock = &wq->list_lock;
  222. unsigned long flags;
  223. while (1) {
  224. spin_lock_irqsave(lock, flags);
  225. if (list_empty(list))
  226. break;
  227. work = list_entry(list->next, struct btrfs_work,
  228. ordered_list);
  229. if (!test_bit(WORK_DONE_BIT, &work->flags))
  230. break;
  231. /*
  232. * we are going to call the ordered done function, but
  233. * we leave the work item on the list as a barrier so
  234. * that later work items that are done don't have their
  235. * functions called before this one returns
  236. */
  237. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  238. break;
  239. trace_btrfs_ordered_sched(work);
  240. spin_unlock_irqrestore(lock, flags);
  241. work->ordered_func(work);
  242. /* now take the lock again and drop our item from the list */
  243. spin_lock_irqsave(lock, flags);
  244. list_del(&work->ordered_list);
  245. spin_unlock_irqrestore(lock, flags);
  246. /*
  247. * we don't want to call the ordered free functions
  248. * with the lock held though
  249. */
  250. work->ordered_free(work);
  251. trace_btrfs_all_work_done(work);
  252. }
  253. spin_unlock_irqrestore(lock, flags);
  254. }
  255. static void normal_work_helper(struct btrfs_work *work)
  256. {
  257. struct __btrfs_workqueue *wq;
  258. int need_order = 0;
  259. /*
  260. * We should not touch things inside work in the following cases:
  261. * 1) after work->func() if it has no ordered_free
  262. * Since the struct is freed in work->func().
  263. * 2) after setting WORK_DONE_BIT
  264. * The work may be freed in other threads almost instantly.
  265. * So we save the needed things here.
  266. */
  267. if (work->ordered_func)
  268. need_order = 1;
  269. wq = work->wq;
  270. trace_btrfs_work_sched(work);
  271. thresh_exec_hook(wq);
  272. work->func(work);
  273. if (need_order) {
  274. set_bit(WORK_DONE_BIT, &work->flags);
  275. run_ordered_work(wq);
  276. }
  277. if (!need_order)
  278. trace_btrfs_all_work_done(work);
  279. }
  280. void btrfs_init_work(struct btrfs_work *work, btrfs_work_func_t uniq_func,
  281. btrfs_func_t func,
  282. btrfs_func_t ordered_func,
  283. btrfs_func_t ordered_free)
  284. {
  285. work->func = func;
  286. work->ordered_func = ordered_func;
  287. work->ordered_free = ordered_free;
  288. INIT_WORK(&work->normal_work, uniq_func);
  289. INIT_LIST_HEAD(&work->ordered_list);
  290. work->flags = 0;
  291. }
  292. static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
  293. struct btrfs_work *work)
  294. {
  295. unsigned long flags;
  296. work->wq = wq;
  297. thresh_queue_hook(wq);
  298. if (work->ordered_func) {
  299. spin_lock_irqsave(&wq->list_lock, flags);
  300. list_add_tail(&work->ordered_list, &wq->ordered_list);
  301. spin_unlock_irqrestore(&wq->list_lock, flags);
  302. }
  303. trace_btrfs_work_queued(work);
  304. queue_work(wq->normal_wq, &work->normal_work);
  305. }
  306. void btrfs_queue_work(struct btrfs_workqueue *wq,
  307. struct btrfs_work *work)
  308. {
  309. struct __btrfs_workqueue *dest_wq;
  310. if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
  311. dest_wq = wq->high;
  312. else
  313. dest_wq = wq->normal;
  314. __btrfs_queue_work(dest_wq, work);
  315. }
  316. static inline void
  317. __btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
  318. {
  319. destroy_workqueue(wq->normal_wq);
  320. trace_btrfs_workqueue_destroy(wq);
  321. kfree(wq);
  322. }
  323. void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
  324. {
  325. if (!wq)
  326. return;
  327. if (wq->high)
  328. __btrfs_destroy_workqueue(wq->high);
  329. __btrfs_destroy_workqueue(wq->normal);
  330. kfree(wq);
  331. }
  332. void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
  333. {
  334. if (!wq)
  335. return;
  336. wq->normal->limit_active = limit_active;
  337. if (wq->high)
  338. wq->high->limit_active = limit_active;
  339. }
  340. void btrfs_set_work_high_priority(struct btrfs_work *work)
  341. {
  342. set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
  343. }