async-thread.c 11 KB

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