async-thread.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. * Copyright (C) 2014 Fujitsu. All rights reserved.
  5. */
  6. #include <linux/kthread.h>
  7. #include <linux/slab.h>
  8. #include <linux/list.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/freezer.h>
  11. #include "async-thread.h"
  12. #include "ctree.h"
  13. enum {
  14. WORK_DONE_BIT,
  15. WORK_ORDER_DONE_BIT,
  16. };
  17. #define NO_THRESHOLD (-1)
  18. #define DFT_THRESHOLD (32)
  19. struct btrfs_workqueue {
  20. struct workqueue_struct *normal_wq;
  21. /* File system this workqueue services */
  22. struct btrfs_fs_info *fs_info;
  23. /* List head pointing to ordered work list */
  24. struct list_head ordered_list;
  25. /* Spinlock for ordered_list */
  26. spinlock_t list_lock;
  27. /* Thresholding related variants */
  28. atomic_t pending;
  29. /* Up limit of concurrency workers */
  30. int limit_active;
  31. /* Current number of concurrency workers */
  32. int current_active;
  33. /* Threshold to change current_active */
  34. int thresh;
  35. unsigned int count;
  36. spinlock_t thres_lock;
  37. };
  38. struct btrfs_fs_info * __pure btrfs_workqueue_owner(const struct btrfs_workqueue *wq)
  39. {
  40. return wq->fs_info;
  41. }
  42. struct btrfs_fs_info * __pure btrfs_work_owner(const struct btrfs_work *work)
  43. {
  44. return work->wq->fs_info;
  45. }
  46. bool btrfs_workqueue_normal_congested(const struct btrfs_workqueue *wq)
  47. {
  48. /*
  49. * We could compare wq->pending with num_online_cpus()
  50. * to support "thresh == NO_THRESHOLD" case, but it requires
  51. * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's
  52. * postpone it until someone needs the support of that case.
  53. */
  54. if (wq->thresh == NO_THRESHOLD)
  55. return false;
  56. return atomic_read(&wq->pending) > wq->thresh * 2;
  57. }
  58. struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
  59. const char *name, unsigned int flags,
  60. int limit_active, int thresh)
  61. {
  62. struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
  63. if (!ret)
  64. return NULL;
  65. ret->fs_info = fs_info;
  66. ret->limit_active = limit_active;
  67. atomic_set(&ret->pending, 0);
  68. if (thresh == 0)
  69. thresh = DFT_THRESHOLD;
  70. /* For low threshold, disabling threshold is a better choice */
  71. if (thresh < DFT_THRESHOLD) {
  72. ret->current_active = limit_active;
  73. ret->thresh = NO_THRESHOLD;
  74. } else {
  75. /*
  76. * For threshold-able wq, let its concurrency grow on demand.
  77. * Use minimal max_active at alloc time to reduce resource
  78. * usage.
  79. */
  80. ret->current_active = 1;
  81. ret->thresh = thresh;
  82. }
  83. ret->normal_wq = alloc_workqueue("btrfs-%s", flags, ret->current_active,
  84. name);
  85. if (!ret->normal_wq) {
  86. kfree(ret);
  87. return NULL;
  88. }
  89. INIT_LIST_HEAD(&ret->ordered_list);
  90. spin_lock_init(&ret->list_lock);
  91. spin_lock_init(&ret->thres_lock);
  92. trace_btrfs_workqueue_alloc(ret, name);
  93. return ret;
  94. }
  95. /*
  96. * Hook for threshold which will be called in btrfs_queue_work.
  97. * This hook WILL be called in IRQ handler context,
  98. * so workqueue_set_max_active MUST NOT be called in this hook
  99. */
  100. static inline void thresh_queue_hook(struct btrfs_workqueue *wq)
  101. {
  102. if (wq->thresh == NO_THRESHOLD)
  103. return;
  104. atomic_inc(&wq->pending);
  105. }
  106. /*
  107. * Hook for threshold which will be called before executing the work,
  108. * This hook is called in kthread content.
  109. * So workqueue_set_max_active is called here.
  110. */
  111. static inline void thresh_exec_hook(struct btrfs_workqueue *wq)
  112. {
  113. int new_current_active;
  114. long pending;
  115. int need_change = 0;
  116. if (wq->thresh == NO_THRESHOLD)
  117. return;
  118. atomic_dec(&wq->pending);
  119. spin_lock(&wq->thres_lock);
  120. /*
  121. * Use wq->count to limit the calling frequency of
  122. * workqueue_set_max_active.
  123. */
  124. wq->count++;
  125. wq->count %= (wq->thresh / 4);
  126. if (!wq->count)
  127. goto out;
  128. new_current_active = wq->current_active;
  129. /*
  130. * pending may be changed later, but it's OK since we really
  131. * don't need it so accurate to calculate new_max_active.
  132. */
  133. pending = atomic_read(&wq->pending);
  134. if (pending > wq->thresh)
  135. new_current_active++;
  136. if (pending < wq->thresh / 2)
  137. new_current_active--;
  138. new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
  139. if (new_current_active != wq->current_active) {
  140. need_change = 1;
  141. wq->current_active = new_current_active;
  142. }
  143. out:
  144. spin_unlock(&wq->thres_lock);
  145. if (need_change) {
  146. workqueue_set_max_active(wq->normal_wq, wq->current_active);
  147. }
  148. }
  149. static void run_ordered_work(struct btrfs_workqueue *wq,
  150. struct btrfs_work *self)
  151. {
  152. struct list_head *list = &wq->ordered_list;
  153. struct btrfs_work *work;
  154. spinlock_t *lock = &wq->list_lock;
  155. unsigned long flags;
  156. bool free_self = false;
  157. while (1) {
  158. spin_lock_irqsave(lock, flags);
  159. if (list_empty(list))
  160. break;
  161. work = list_entry(list->next, struct btrfs_work,
  162. ordered_list);
  163. if (!test_bit(WORK_DONE_BIT, &work->flags))
  164. break;
  165. /*
  166. * Orders all subsequent loads after reading WORK_DONE_BIT,
  167. * paired with the smp_mb__before_atomic in btrfs_work_helper
  168. * this guarantees that the ordered function will see all
  169. * updates from ordinary work function.
  170. */
  171. smp_rmb();
  172. /*
  173. * we are going to call the ordered done function, but
  174. * we leave the work item on the list as a barrier so
  175. * that later work items that are done don't have their
  176. * functions called before this one returns
  177. */
  178. if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
  179. break;
  180. trace_btrfs_ordered_sched(work);
  181. spin_unlock_irqrestore(lock, flags);
  182. work->ordered_func(work);
  183. /* now take the lock again and drop our item from the list */
  184. spin_lock_irqsave(lock, flags);
  185. list_del(&work->ordered_list);
  186. spin_unlock_irqrestore(lock, flags);
  187. if (work == self) {
  188. /*
  189. * This is the work item that the worker is currently
  190. * executing.
  191. *
  192. * The kernel workqueue code guarantees non-reentrancy
  193. * of work items. I.e., if a work item with the same
  194. * address and work function is queued twice, the second
  195. * execution is blocked until the first one finishes. A
  196. * work item may be freed and recycled with the same
  197. * work function; the workqueue code assumes that the
  198. * original work item cannot depend on the recycled work
  199. * item in that case (see find_worker_executing_work()).
  200. *
  201. * Note that different types of Btrfs work can depend on
  202. * each other, and one type of work on one Btrfs
  203. * filesystem may even depend on the same type of work
  204. * on another Btrfs filesystem via, e.g., a loop device.
  205. * Therefore, we must not allow the current work item to
  206. * be recycled until we are really done, otherwise we
  207. * break the above assumption and can deadlock.
  208. */
  209. free_self = true;
  210. } else {
  211. /*
  212. * We don't want to call the ordered free functions with
  213. * the lock held.
  214. */
  215. work->ordered_free(work);
  216. /* NB: work must not be dereferenced past this point. */
  217. trace_btrfs_all_work_done(wq->fs_info, work);
  218. }
  219. }
  220. spin_unlock_irqrestore(lock, flags);
  221. if (free_self) {
  222. self->ordered_free(self);
  223. /* NB: self must not be dereferenced past this point. */
  224. trace_btrfs_all_work_done(wq->fs_info, self);
  225. }
  226. }
  227. static void btrfs_work_helper(struct work_struct *normal_work)
  228. {
  229. struct btrfs_work *work = container_of(normal_work, struct btrfs_work,
  230. normal_work);
  231. struct btrfs_workqueue *wq = work->wq;
  232. int need_order = 0;
  233. /*
  234. * We should not touch things inside work in the following cases:
  235. * 1) after work->func() if it has no ordered_free
  236. * Since the struct is freed in work->func().
  237. * 2) after setting WORK_DONE_BIT
  238. * The work may be freed in other threads almost instantly.
  239. * So we save the needed things here.
  240. */
  241. if (work->ordered_func)
  242. need_order = 1;
  243. trace_btrfs_work_sched(work);
  244. thresh_exec_hook(wq);
  245. work->func(work);
  246. if (need_order) {
  247. /*
  248. * Ensures all memory accesses done in the work function are
  249. * ordered before setting the WORK_DONE_BIT. Ensuring the thread
  250. * which is going to executed the ordered work sees them.
  251. * Pairs with the smp_rmb in run_ordered_work.
  252. */
  253. smp_mb__before_atomic();
  254. set_bit(WORK_DONE_BIT, &work->flags);
  255. run_ordered_work(wq, work);
  256. } else {
  257. /* NB: work must not be dereferenced past this point. */
  258. trace_btrfs_all_work_done(wq->fs_info, work);
  259. }
  260. }
  261. void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,
  262. btrfs_func_t ordered_func, btrfs_func_t ordered_free)
  263. {
  264. work->func = func;
  265. work->ordered_func = ordered_func;
  266. work->ordered_free = ordered_free;
  267. INIT_WORK(&work->normal_work, btrfs_work_helper);
  268. INIT_LIST_HEAD(&work->ordered_list);
  269. work->flags = 0;
  270. }
  271. void btrfs_queue_work(struct btrfs_workqueue *wq, struct btrfs_work *work)
  272. {
  273. unsigned long flags;
  274. work->wq = wq;
  275. thresh_queue_hook(wq);
  276. if (work->ordered_func) {
  277. spin_lock_irqsave(&wq->list_lock, flags);
  278. list_add_tail(&work->ordered_list, &wq->ordered_list);
  279. spin_unlock_irqrestore(&wq->list_lock, flags);
  280. }
  281. trace_btrfs_work_queued(work);
  282. queue_work(wq->normal_wq, &work->normal_work);
  283. }
  284. void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
  285. {
  286. if (!wq)
  287. return;
  288. destroy_workqueue(wq->normal_wq);
  289. trace_btrfs_workqueue_destroy(wq);
  290. kfree(wq);
  291. }
  292. void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
  293. {
  294. if (wq)
  295. wq->limit_active = limit_active;
  296. }
  297. void btrfs_flush_workqueue(struct btrfs_workqueue *wq)
  298. {
  299. flush_workqueue(wq->normal_wq);
  300. }